*hash is different from hash.to_a?

T

Trans

Has this come up before? I don;t recall. But just the same. Does this
seem inconsistent to you?

irb(main):003:0> a = *{:a=>1}
=> [:a, 1]
irb(main):004:0> a = *{:a=>1,:b=>2}
=> [[:b, 2], [:a, 1]]

vs.

irb(main):006:0> a = {:a=>1}.to_a
=> [[:a, 1]]
irb(main):007:0> a = {:a=>1,:b=>2}.to_a
=> [[:b, 2], [:a, 1]]

T.
 
A

Alex Young

Trans said:
Has this come up before? I don;t recall. But just the same. Does this
seem inconsistent to you?

irb(main):003:0> a = *{:a=>1}
=> [:a, 1]
irb(main):004:0> a = *{:a=>1,:b=>2}
=> [[:b, 2], [:a, 1]]

vs.

irb(main):006:0> a = {:a=>1}.to_a
=> [[:a, 1]]
irb(main):007:0> a = {:a=>1,:b=>2}.to_a
=> [[:b, 2], [:a, 1]]

Not really inconsistent - * unwraps its argument if it can. Observe:

irb(main):001:0> a = *[1,2]
=> [1, 2]
irb(main):002:0> a = *[1]
=> 1
 
M

MonkeeSage

Has this come up before? I don;t recall. But just the same. Does this
seem inconsistent to you?

irb(main):003:0> a = *{:a=>1}
=> [:a, 1]
irb(main):004:0> a = *{:a=>1,:b=>2}
=> [[:b, 2], [:a, 1]]

vs.

irb(main):006:0> a = {:a=>1}.to_a
=> [[:a, 1]]
irb(main):007:0> a = {:a=>1,:b=>2}.to_a
=> [[:b, 2], [:a, 1]]

T.

Splat behavior is going to be slightly different in 1.9 [1] (e.g.,
splat just returns the whole hash, regardless of element count, inside
a single array). Just thought you might like to know so you don't end
up writing brand new legacy code. ;)

[1] http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/214897?214708-244931

Regards,
Jordan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top