Hash enumeration returning nulls?

Discussion in 'Ruby' started by cczona, Mar 26, 2010.

  1. cczona

    cczona Guest

    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.
     
    cczona, Mar 26, 2010
    #1
    1. Advertising

  2. On Fri, Mar 26, 2010 at 12:45 PM, cczona <> 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
     
    Rick DeNatale, Mar 26, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. puvit82
    Replies:
    4
    Views:
    768
    puvit82
    Feb 1, 2008
  2. Alf P. Steinbach /Usenet

    Re: Returning Nulls in Templates

    Alf P. Steinbach /Usenet, Mar 20, 2011, in forum: C++
    Replies:
    7
    Views:
    276
    James Kanze
    Mar 26, 2011
  3. Paul
    Replies:
    15
    Views:
    425
    James Kanze
    Mar 28, 2011
  4. Tobias Lekman

    Returning Enumeration or Exception from WebMethod?

    Tobias Lekman, Aug 9, 2003, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    166
    Marina
    Aug 11, 2003
  5. rp
    Replies:
    1
    Views:
    555
    red floyd
    Nov 10, 2011
Loading...

Share This Page