a large hash
h = { … }
1. this is inefficient
first_key, first_value = h.keys[0], h.values[0]
also, #1 is not thread-safe right?
is there a #pairs method which returns [[k1,v1], [k2,v2], …} or
something similar?
2. will this be compatible with future ruby?
h.each_pair {|k,v| first_key, first_value = k, v; break}
···
–
dave
HAL_9000
(HAL 9000)
2
David Garamond wrote:
a large hash
h = { … }
1. this is inefficient
first_key, first_value = h.keys[0], h.values[0]
also, #1 is not thread-safe right?
is there a #pairs method which returns [[k1,v1], [k2,v2], …} or
something similar?
2. will this be compatible with future ruby?
h.each_pair {|k,v| first_key, first_value = k, v; break}
Hmm, how about:
k,v = h.find { true } # Find first obj for which block is true
Hal
Vaine
3
David Garamond lists@zara.6.isreserved.com wrote in message news:40025921.4090603@zara.6.isreserved.com…
is there a #pairs method which returns [[k1,v1], [k2,v2], …} or
something similar?
Hash#to_a does just that:
irb(main):003:0> {:a => 1, :b => 2}.to_a
=> [[:a, 1], [:b, 2]]
Ville Aine wrote:
David Garamond lists@zara.6.isreserved.com wrote in message news:40025921.4090603@zara.6.isreserved.com…
is there a #pairs method which returns [[k1,v1], [k2,v2], …} or
something similar?
Hash#to_a does just that:
irb(main):003:0> {:a => 1, :b => 2}.to_a
=> [[:a, 1], [:b, 2]]
yes, but converting the whole hash to an array is inefficient (for big
hashes), that’s why he doesn’t want to use this.