Hash enumeration returning nulls?

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.

On Fri, Mar 26, 2010 at 12:45 PM, cczona [email protected] wrote:

puts “\n\n”

$foo.each do |k, v|
print $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={‘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”
end

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs