Skip to content

Instantly share code, notes, and snippets.

@JoshMcKin
Created October 4, 2010 15:13
Show Gist options
  • Save JoshMcKin/609851 to your computer and use it in GitHub Desktop.
Save JoshMcKin/609851 to your computer and use it in GitHub Desktop.
class Foo
attr_accessor :test
def self.new_with_params(params={})
f = Foo.new
params.each do |param|
# if method exists send value
f.send(param[0].to_s + "=",param[1]) if f.respond_to?(param[0].to_s)
end
f
end
end
# Usage
bar = Foo.new_with_params(:test => 1)
bar.test # puts 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment