Skip to content

Instantly share code, notes, and snippets.

@h3h
Last active August 29, 2015 13:57
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 h3h/9725898 to your computer and use it in GitHub Desktop.
Save h3h/9725898 to your computer and use it in GitHub Desktop.
Accessing multiple attributes/methods of an object with a concise syntax.
module Attrlist
def /(attrlist)
attrlist.map { |a| send(a) }
end
end
class Time
include Attrlist
end
# Use an attribute list to access multiple attributes of an object
# at once, receiving the values back as an array.
#
# == Example Usage
#
# >> t = Time.new
# => 2014-03-23 11:44:05 -0500
#
# >> t/[:year, :month, :day]
# => [2014, 3, 23]
@h3h
Copy link
Author

h3h commented Mar 23, 2014

So, for instance, if you wanted to create a new Time object with only a couple new specific attributes:

>> specific_time = Time.new(*t/[:year, :month, :day], hour, minute)
=> 2014-03-23 09:34:00 -0500  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment