I'm trying to get the keys, but why values appear?

Discussion in 'Ruby' started by Abder-rahman Ali, Jul 13, 2010.

  1. Abder-rahman Ali, Jul 13, 2010
    #1
    1. Advertising

  2. On Tuesday 13 July 2010, Abder-rahman Ali wrote:
    > |I'm trying to write a script that lists ONLY the keys in a hash as
    > |follows: http://pastie.org/private/1ruahb5w05ihsloqwmqeng
    > |
    > |The case is that when I run the script, I get the following:
    > |
    > |NameAbder-Rahman Ali
    > |Age26
    > |
    > |While, I'm intending to get
    > |
    > |Name
    > |Age
    > |
    > |What am I missing in the script?
    > |
    > |Thanks.


    As you can see from the documentation (for example, with ri Hash#each),
    Hash#each yields two arguments to the block: the key and the corresponding
    value. Since your block only takes one argument, ruby puts both in an array,
    whose to_s method produces a string with the two elements one after the other.

    If you only want the key, you can use each_key rather than each, since this
    will only pass the block the keys:

    personal_information.each_key{|k| puts k}

    If you want to use each (but you don't need to in your example), you can have
    the block take two parameters and only use the first (the key):

    personal_information.each{|key, val| puts key}

    or have the block take a single argument (which will be an array) and use only
    the first entry of the array

    personal_information.each{|i| puts i[0]}

    I hope this helps

    Stefano
    Stefano Crocco, Jul 13, 2010
    #2
    1. Advertising

  3. Abder-rahman Ali

    botp Guest

    On Tue, Jul 13, 2010 at 4:40 PM, Abder-rahman Ali
    <> wrote:
    > What am I missing in the script?


    the fun :)

    > personal_information.each {|key| p key}

    ["Name", "Abder-Rahman Ali"]
    ["Age", "26"]

    > personal_information.each {|key| p key.first}

    "Name"
    "Age"

    > personal_information.each_key {|key| p key}

    "Name"
    "Age"

    > personal_information.each_value {|key| p key}

    "Abder-Rahman Ali"
    "26"

    > personal_information.each {|key,value| p key}

    "Name"
    "Age"

    > personal_information.each {|key,value| p value}

    "Abder-Rahman Ali"
    "26"


    kind regards -botp
    botp, Jul 13, 2010
    #3
  4. If you want the keys only you can do this
    "personal_information.keys.each do |key|" or
    "personal_information.each_key do |key|"

    This, however, "personal_information.each do |key|" returns each
    element of the hash as an array in the form [key, value]

    Look here for the documentation (
    http://www.ruby-doc.org/core/classes/Hash.html ), in fact this should
    be the first place you look when you have a problem.
    Peter Hickman, Jul 13, 2010
    #4
  5. Abder-Rahman Ali, Jul 13, 2010
    #5
    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. Replies:
    5
    Views:
    362
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    855
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,747
    Smokey Grindel
    Dec 2, 2006
  4. sauce
    Replies:
    0
    Views:
    223
    sauce
    Apr 12, 2005
  5. yang cao
    Replies:
    0
    Views:
    201
    yang cao
    Apr 9, 2007
Loading...

Share This Page