Skip to content

Instantly share code, notes, and snippets.

@jqr
Created March 8, 2009 19:19
Show Gist options
  • Save jqr/75874 to your computer and use it in GitHub Desktop.
Save jqr/75874 to your computer and use it in GitHub Desktop.
# 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