Skip to content

Instantly share code, notes, and snippets.

@h3h h3h/object_attrlist.rb
Last active Aug 29, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.