Skip to content

Instantly share code, notes, and snippets.

@kalleth
Created July 19, 2012 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalleth/3146291 to your computer and use it in GitHub Desktop.
Save kalleth/3146291 to your computer and use it in GitHub Desktop.
getting attrs
class MyClass
attr_accessor :at1, :at2, :at3
def initialize
@at1 = "one"
@at2 = "two"
@at3 = "three"
end
def my_attrs(*attrs)
{}.tap do |h|
attrs.each {|a| h[a] = self.send(a)}
end
end
end
irb(main):048:0> m = MyClass.new
=> #<MyClass:0x2b22010 @at1="one", @at2="two", @at3="three">
irb(main):049:0> m.my_attrs("at1")
=> {"at1"=>"one"}
irb(main):050:0> m.my_attrs("at1", "at2")
=> {"at1"=>"one", "at2"=>"two"}
irb(main):051:0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment