Skip to content

Instantly share code, notes, and snippets.

@hypomodern
Forked from cadwallion/gist:2316695
Created April 6, 2012 04:06
Show Gist options
  • Save hypomodern/2316750 to your computer and use it in GitHub Desktop.
Save hypomodern/2316750 to your computer and use it in GitHub Desktop.
Array() shenanigans...
>> Array([])
=> []
>> Array(nil)
=> []
>> Array([{foo: 'bar'}])
=> [{:foo=>"bar"}]
>> Array([{foo: 'bar'}, {bar: 'baz'}])
=> [{:foo=>"bar"}, {:bar=>"baz"}]
>> Array({foo: 'bar'})
=> [[:foo, "bar"]]
# SIDE EFFECTS MAY OCCUR :)
1.9.3p0 :020 > class Hash
1.9.3p0 :021?> def to_ary
1.9.3p0 :022?> [self]
1.9.3p0 :023?> end
1.9.3p0 :024?> end
1.9.3p0 :026 > Array({ foo: 6 })
=> [{:foo=>6}]
1.9.3p0 :029 > hsh = { baz: nil }
=> {:baz=>nil}
1.9.3p0 :030 > hsh[:baz] = Array(hsh[:baz]) << { foo: 6 }
=> [{:foo=>6}]
1.9.3p0 :031 > hsh
=> {:baz=>[{:foo=>6}]}
1.9.3p0 :032 > hsh[:baz] = Array(hsh[:baz]) << { foo: 9 }
=> [{:foo=>6}, {:foo=>9}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment