0

I have following code in a test.rb file:

hello = { :credit => "Testing" }
acc = ":credit"
puts hello[a.to_sym]

When I run it as: ruby test.rb, I am supposed to get the value of the Hash element (Testing) but I am getting nothing.

What am I doing wrong? Thanks in advance for the answer.

1 Answer 1

4

The colon in the string is what is screwing you up.

1.9.3p429 :003 > acc.to_sym
:":credit"

You need to make it just "credit"

1.9.3p429 :004 > acc = "credit"
"credit"
1.9.3p429 :005 > acc.to_sym
:credit
1.9.3p429 :006 > hello[acc.to_sym]
"Testing"
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! thanks for the quick answer Eugene. It worked! Shame on me, I did a silly mistake. :)

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.