On Fri, Mar 26, 2010 at 12:45 PM, cczona <cczona / gmail.com> wrote:
> 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