Confused in array as key

I try to implement array as a key with following example:

dictionary = { 'cat' => 'feline animal', 'dog' => 'canine animal'}

And I got output like:

feline animal
canine animal

But how is it come? Means displayed output is a value of key element in
Hash BUT for output I used key in array like "-------------puts
dictionary[key]".

Could anyone explain me?

Thank you.

···

--
Posted via http://www.ruby-forum.com/.

Dear Jaimin,

Your question is really confusing.
Could you please try to "rephrase it" (say it another way) showing
code examples so we can get a grab of what you're trying to
accomplish?

I would give my try that you are talking about "array as key" as in
the foreach from PHP? Is it something about it?

dictionary = { 'cat' => 'feline animal', 'dog' => 'canine animal'}

dictionary.class
# => Hash

# So It's a Hash, not an Array.

dictionary.keys
# => ["cat", "dog"]

dictionary.values
=> ["feline animal", "canine animal"]

dictionary.each do |key, value|
  puts "The key is #{key} and the value is #{value}"
end

···

On Sun, Mar 9, 2014 at 7:18 AM, Jaimin Pandya <lists@ruby-forum.com> wrote:

I try to implement array as a key with following example:

dictionary = { 'cat' => 'feline animal', 'dog' => 'canine animal'}

And I got output like:

feline animal
canine animal

But how is it come? Means displayed output is a value of key element in
Hash BUT for output I used key in array like "-------------puts
dictionary[key]".

Could anyone explain me?

Thank you.

--
Posted via http://www.ruby-forum.com/\.

# Sorry for the cut e-mail
# going on...

dictionary.each do |key, value|
  puts "The key is #{key} and the value is #{value}"
end

# It outputs

The key is cat and the value is feline animal
The key is dog and the value is canine animal

Best regards,
Abinoam Jr.

···

On Sun, Mar 9, 2014 at 9:23 AM, Abinoam Jr. <abinoam@gmail.com> wrote:

Dear Jaimin,

Your question is really confusing.
Could you please try to "rephrase it" (say it another way) showing
code examples so we can get a grab of what you're trying to
accomplish?

I would give my try that you are talking about "array as key" as in
the foreach from PHP? Is it something about it?

dictionary = { 'cat' => 'feline animal', 'dog' => 'canine animal'}

dictionary.class
# => Hash

# So It's a Hash, not an Array.

dictionary.keys
# => ["cat", "dog"]

dictionary.values
=> ["feline animal", "canine animal"]

dictionary.each do |key, value|
  puts "The key is #{key} and the value is #{value}"
end

Although it is not obvious, what you try to achieve, I venture, that you
should read about the method “keys” of the Hash-class :

ri Hash.keys

Do not talk about Hashes as if they were Arrays. Despite all similarity,
you use the one, where you do not want the other and we will understand
you much better.

Please give us an authentic code-example, as your line
puts dictionary[key]
is not authentic. 'key' is not defined, so we cannot know, what you
really do.

TY.

···

--
Posted via http://www.ruby-forum.com/.

Abinoam Jr. wrote in post #1139301:

···

On Sun, Mar 9, 2014 at 9:23 AM, Abinoam Jr. <abinoam@gmail.com> wrote:

dictionary = { 'cat' => 'feline animal', 'dog' => 'canine animal'}
=> ["feline animal", "canine animal"]

dictionary.each do |key, value|
  puts "The key is #{key} and the value is #{value}"
end

# Sorry for the cut e-mail
# going on...

dictionary.each do |key, value|
  puts "The key is #{key} and the value is #{value}"
end

# It outputs

The key is cat and the value is feline animal
The key is dog and the value is canine animal

I rephrase my question.

Thank you

--
Posted via http://www.ruby-forum.com/\.