Hash enumeration returning nulls?

C

cczona

Okay, I must just be too sleep-deprived to see it. Why is Hash.each
(and the other enumerables) returning nothing but nils when clearly
the hash is being populated?

$foo={'a' => '1', 'b'=> '2'}

puts $foo.length
puts $foo.nil?
puts $foo.keys
puts $foo.values
puts "\n\n"

$foo.each do |k, v|
print $k, " and ", $v, "\n\n" # returns 'nil and nil'
end

Thank you.
 
R

Rick DeNatale

Okay, I must just be too sleep-deprived to see it. =A0Why =A0is Hash.each
(and the other enumerables) returning nothing but nils when clearly
the hash is being populated?

$foo=3D{'a' =3D> '1', 'b'=3D> '2'}

puts $foo.length
puts $foo.nil?
puts $foo.keys
puts $foo.values
puts "\n\n"

$foo.each do |k, v|
=A0print $k, " and ", $v, "\n\n" # returns 'nil and nil'
end

Thank you.

$k and $v are global variables, k and v are locals

I rarely use global variables (other than system globals). I'd
rewrite the above code as

foo=3D{'a' =3D> '1', 'b'=3D> '2'}

puts foo.length
puts foo.nil?
puts foo.keys
puts foo.values
puts "\n\n"

foo.each do |k, v|
print k, " and ", v, "\n\n"
end
--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top