Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Hash enumeration returning nulls?

Reply
Thread Tools

Hash enumeration returning nulls?

 
 
cczona
Guest
Posts: n/a
 
      03-26-2010
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.
 
Reply With Quote
 
 
 
 
Rick DeNatale
Guest
Posts: n/a
 
      03-26-2010
On Fri, Mar 26, 2010 at 12:45 PM, cczona <(E-Mail Removed)> 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/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
method returning a hash, is the hash in the heap? Jian Lin Ruby 3 05-20-2010 11:15 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Re-using a simple type definition; with enumeration constraint andwithout enumeration constraint puvit82 XML 4 02-01-2008 03:46 PM
Returning Enumeration or Exception from WebMethod? Tobias Lekman ASP .Net Web Services 1 08-11-2003 05:26 PM



Advertisments