Skip to content

Instantly share code, notes, and snippets.

@invisiblefunnel
Created June 26, 2013 16:16
Show Gist options
  • Select an option

  • Save invisiblefunnel/5868826 to your computer and use it in GitHub Desktop.

Select an option

Save invisiblefunnel/5868826 to your computer and use it in GitHub Desktop.
class User
attr_accessor :name
def initialize(name); @name = name; end
end
class Collection
include Enumerable
def initialize(*elements)
@elements = elements
end
def each(&block)
@elements.each(&block)
end
def pluck(attribute)
map { |element| element.public_send(attribute) }
end
end
class UserCollection < Collection
def names
pluck(:name)
end
end
users = 5.times.map { |n|
User.new("USER#{n}")
}
collection = UserCollection.new(*users)
p collection.names
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment