Skip to content

Instantly share code, notes, and snippets.

@ordinaryzelig
Created March 14, 2010 02:46
Show Gist options
  • Save ordinaryzelig/331733 to your computer and use it in GitHub Desktop.
Save ordinaryzelig/331733 to your computer and use it in GitHub Desktop.
attributes hash for any object
# convert results of calling methods into attributes hash.
# e.g. Date.today.attributes_for(:day, :month, :year) would return
# {:day => 13, :month => 3, :year => 2010}
class Object
def attributes_for(*method_names)
method_names.inject({}) do |hash, method_name|
hash[method_name] = send(method_name)
hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment