22

I want to use an options hash as input to a method in Ruby, but is there a way to quickly set all eponymous variables (i.e having the same name) instead of setting each individually?

So instead of doing the following:

class Connection
  def initialize(opts={})
    @host     = opts[:host]
    @user     = opts[:user]
    @password = opts[:password]
    @project  = opts[:project]
    # ad nauseum...

is there a one-liner that will assign each incoming option in the hash to the variable with the same name?

1

4 Answers 4

52
def initialize(opts={})
  opts.each { |k,v| instance_variable_set("@#{k}", v) }
end
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I just found this: stackoverflow.com/questions/9597249/… which is exactly what you are also saying. I will close my question as a duplicate, but upvote your answer.
Whitelisting the keys would be a good addition, a couple seconds to set up the whitelist now could save hours of debugging time later and help document the interface as a side effect.
4

This should give you what you're looking for:

def initialize(opts={})
  opts.each_pair do |key, value|
    send("#{key}=",value)
  end
end

1 Comment

This only works if you have setter methods via attr_accessor, attr_writer, or manually created setter methods
1

you can do this with,

 @host, @user, @password, @project  = opts[:host], opts[:user], opts[:password], opts[:project]

hope this helps!

Comments

1

Besides Casper's solution, you could also use the instance_variables_from gem:

instance_variables_from(opts)

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.