Forum: Ruby Storing Array in Hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8b3542c2dde0492962e8998cb8164635?d=identicon&s=25 John Bentley (jbentley)
on 2008-11-23 15:17
I am having the most insane time with Ruby's Hash class.

I have Array's of varied length, but whenever they are stored and then
retrieved from a Hash they become length of 1 and all the values are
somehow combined in that first cell.

Code:

aRay = ["a","b","c"]
aRay2 = ["1","2","3","4","5"]

# prints out 3
puts aRay.size
# prints out 5
puts aRay2.size

# store them
h = {"letters" => aRay, "numbers" => aRay2}

# prints out abc
puts h.values_at("letters").to_s
# prints out 12345
puts h.values_at("numbers").to_s

# retrieve the arrays
ltrArray = h.values_at("letters")
numArray = h.values_at("numbers")

# prints out abc
puts ltrArray.to_s
# prints out 12345
puts numArray.to_s

# prints out 1
puts ltrArray.size
# prints out 1
puts numArray.size




What am I doing wrong?
8a85c693f13ef7cb542ef94d2a403d4d?d=identicon&s=25 Luc Heinrich (Guest)
on 2008-11-23 15:23
(Received via mailing list)
On 23 nov. 08, at 15:13, John Bentley wrote:

> puts h.values_at("letters").to_s

Hash#values_at returns an array of the requested values, so in your
case you get an array of array(s).

What you want is:
ltrArray = h.values_at("letters").first
numArray = h.values_at("numbers").first
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2008-11-23 15:27
(Received via mailing list)
Alle Sunday 23 November 2008, John Bentley ha scritto:
>
> # prints out 12345
>
> # prints out 1
> puts ltrArray.size
> # prints out 1
> puts numArray.size
>
>
>
>
> What am I doing wrong?

Try replacing lines like

puts ltrArray.to_s

with

p ltrArray

and you'll understand what's happening.

By the way, you don't need to add the .to_s to the argument of a call to
puts
(or p), as puts will do that for you. Also, to display the contents of
hashes
and arrays, the inspect (and the associated p) method may be more useful
than
the to_s (and associated puts) method.

Stefano
8b3542c2dde0492962e8998cb8164635?d=identicon&s=25 John Bentley (jbentley)
on 2008-11-23 15:28
Luc Heinrich wrote:
> What you want is:
> ltrArray = h.values_at("letters").first
> numArray = h.values_at("numbers").first

Oh wow...I've been plugging away at this for 4 hours now and that just
fixed my problem.  Thank you so much!

I'm in the process of learning Ruby through various online mediums so I
tend to miss little intricacies like that.

Thanks again.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-23 15:51
(Received via mailing list)
On 23.11.2008 15:24, John Bentley wrote:
> Luc Heinrich wrote:
>> What you want is:
>> ltrArray = h.values_at("letters").first
>> numArray = h.values_at("numbers").first
>
> Oh wow...I've been plugging away at this for 4 hours now and that just
> fixed my problem.  Thank you so much!
>
> I'm in the process of learning Ruby through various online mediums so I
> tend to miss little intricacies like that.

A good tool to try these things out is irb which comes with your Ruby
distribution.  Since irb by default uses #inspect (the same that method
"p" uses) to print out objects you can immediately see what's going on.

Kind regards

  robert
This topic is locked and can not be replied to.