Skip to content

Instantly share code, notes, and snippets.

View vladzloteanu's full-sized avatar

Vlad Zloteanu vladzloteanu

View GitHub Profile
@vladzloteanu
vladzloteanu / gist:1834755
Created February 15, 2012 09:31
[ruby] run shell command, read output (from Bundler code)
def sh(cmd, &block)
out, code = sh_with_code(cmd, &block)
code == 0 ? out : raise(out.empty? ? "Running `#{cmd}' failed. Run this command directly for more detailed output." : out)
end
def sh_with_code(cmd, &block)
cmd << " 2>&1"
outbuf = ''
Bundler.ui.debug(cmd)
Dir.chdir(base) {
@vladzloteanu
vladzloteanu / gist:1767050
Created February 8, 2012 09:15
Inheritance: Class variables and constants in Ruby
class C1
SOME_CONST=3
@@some_class_var = 3
end
class C2<C1
SOME_CONST=7
end
class C3<C1
@vladzloteanu
vladzloteanu / gist:1286780
Created October 14, 2011 10:33
Replace newlines in a file (using tr or perl)
perl -i -p -e 's/\n//' file
tr -d '\n' < file
def test_auto_closing_tags
reader = Nokogiri::XML::Reader.from_memory(<<-eoxml)
<xml><city>Paris</city><state/></xml>
eoxml
assert_equal [false, false, nil, nil, true, nil],
reader.map{|node| node.empty_element? if node.node_type == Nokogiri::XML::Node::ELEMENT_NODE}
end