1

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  

1 Answer 1

6

Did you mean this?:

hash = {"A" =>[1,2,3,4]}
hash["A"][0] #=> 1
hash["A"].include? 4 #=> true
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you...I didn't know you could do that!
Well, hash["A"] will give you the value at key "A" in hash which is an array. That means you can use any array operations on hash["A"].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.