Last active
August 29, 2015 13:57
-
-
Save h3h/9725898 to your computer and use it in GitHub Desktop.
Accessing multiple attributes/methods of an object with a concise syntax.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So, for instance, if you wanted to create a new
Time
object with only a couple new specific attributes: