Lasonic
Lasonic

Reputation: 901

Retrieving an element of an array that is a value of a hash with Ruby

I have the following hash:

hash = {"A" =>[1,2,3,4]}

Within that hash is a key "A" with the value of [1,2,3,4].

Is there a possible way to access a single element within my array using the key-value pair?

Example (...yes I know this isn't legal Ruby):

hash["A",0] => 1

Or have the ability to see if the array included a value with the key-value pair?

hash["A".include? 4] => true  

Upvotes: 1

Views: 72

Answers (1)

Surya
Surya

Reputation: 16022

Did you mean this?:

hash = {"A" =>[1,2,3,4]}
hash["A"][0] #=> 1
hash["A"].include? 4 #=> true

Upvotes: 6

Related Questions