-1

In Javascript properties can be accessed directly with barewords:

var evil = {"knievel":"evel","daredevil":"fearless"}
evil["knievel"] // "evel"
evil.knievel    // "evel"

Is there anything similar in Ruby without having to do evil["knievel"]?

2
  • Really, why does this have a -1? (I'm out of votes or I'd fix the situation.) Commented Sep 9, 2011 at 22:47
  • Ruby does all sorts of wonderful things may be there was a short cut of some sort where keys could be accessed as methods and return values. Commented Sep 9, 2011 at 23:54

1 Answer 1

2
require "ostruct"

evil = OpenStruct.new(:knievel => "evel",:daredevil => "fearless")
puts evil.knievel
Sign up to request clarification or add additional context in comments.

Comments

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.