DevHeads.net

Hash["string1, string2"]

Hello Ruby Friends!

I was reviewing Ruby docs and re-learned that a Hash can be created from
what appears to be a list of strings with the requirement that there is
even number.

I try it in irb:

irb(main):004:0> Hash["stringA1", "stringA2", "stringB1", "stringB2"]
=> {"stringA1"=>"stringA2", "stringB1"=>"stringB2"}

Yet when I try to automate it with a variable I see that the argument of
list appears to be unacceptable.

In irb:

irb(main):007:0> s = ["stringA1", "stringA2", "stringB1", "stringB2"]
=> ["stringA1", "stringA2", "stringB1", "stringB2"]
irb(main):008:0> Hash[s]
(irb):8: warning: wrong element type String at 0 (expected array)
(irb):8: warning: ignoring wrong elements is deprecated, remove them
explicitly
(irb):8: warning: this causes ArgumentError in the next release
(irb):8: warning: wrong element type String at 1 (expected array)
(irb):8: warning: ignoring wrong elements is deprecated, remove them
explicitly
(irb):8: warning: this causes ArgumentError in the next release
(irb):8: warning: wrong element type String at 2 (expected array)
(irb):8: warning: ignoring wrong elements is deprecated, remove them
explicitly
(irb):8: warning: this causes ArgumentError in the next release
(irb):8: warning: wrong element type String at 3 (expected array)
(irb):8: warning: ignoring wrong elements is deprecated, remove them
explicitly
(irb):8: warning: this causes ArgumentError in the next release
=> {}

Yet, this form works when I use variables for each comma separated value.

irb(main):009:0> s1 = "stringA1"
=> "stringA1"
irb(main):010:0> s2 = "stringA2"
=> "stringA2"
irb(main):011:0> s3 = "stringB1"
=> "stringB1"
irb(main):012:0> s4 = "stringB2"
=> "stringB2"
irb(main):013:0> Hash[s1, s2, s3, s4]
=> {"stringA1"=>"stringA2", "stringB1"=>"stringB2"}

This form form of initialization, Hash[] shown here:
<a href="https://ruby-doc.org/core-2.5.1/Hash.html#5B-5D-method" title="https://ruby-doc.org/core-2.5.1/Hash.html#5B-5D-method">https://ruby-doc.org/core-2.5.1/Hash.html#5B-5D-method</a>

Hash["a", 100, "b", 200] # Form 1
Hash[ [ ["a", 100], ["b", 200] ] ] # Form 2
Hash["a" => 100, "b" => 200] # Form 3
=> {"a"=>100, "b"=>200}

I believe the 2nd irb session is showing me the 2nd form of Hash[] failing,
but I am not sure.

How can I use a variable to automate the first form if I did not know how
many pairs will be sent to Hash initialization class method (between the
square braces)?

Of course I could write a method to massage these to conform to the second
prototype from the docs. Is that the best practice and what people
congenitally do?

What is a common use case for the first form?

URLs are welcome for extending reading.

So many thanks!
-az

Comments

Re: Hash["string1, string2"]

By Carlo E. Prelz at 06/12/2018 - 02:08

Quoting Artie Ziff (<a href="mailto:artie. ... at gmail dot com">artie. ... at gmail dot com</a>):

You must do

Hash[*s]

instead. With your version you pass an array, not the single elements,
to the Hash::[] class function

Carlo