Created
March 8, 2009 19:19
-
-
Save jqr/75874 to your computer and use it in GitHub Desktop.
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
# jQuery style method chaining https://twitter.com/grossberg/status/1297223439 | |
class Thing | |
attr_accessor :name | |
def initialize(name) | |
self.name = name | |
end | |
def announce | |
puts "I am #{name}!" | |
end | |
def die | |
puts "Here lies #{name}." | |
end | |
end | |
module Enumerable | |
def method_missing(method, *args) | |
each do |value| | |
value.send(method, *args) | |
end | |
self | |
end | |
end | |
things = [Thing.new('one'), Thing.new('two'), Thing.new('three')] | |
things.announce.die | |
# I am one! | |
# I am two! | |
# I am three! | |
# Here lies one. | |
# Here lies two. | |
# Here lies three. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment