Skip to content

Instantly share code, notes, and snippets.

@fnando
Created January 28, 2011 21:26
Show Gist options
  • Save fnando/800989 to your computer and use it in GitHub Desktop.
Save fnando/800989 to your computer and use it in GitHub Desktop.
Fluent interfaces + blocks example
class Message
def initialize(&block)
instance_eval(&block) if block_given?
end
def to(name)
@to = name
self
end
def from(name)
@from = name
self
end
def text(message)
@text = message
self
end
def deliver
puts "#{@from} said: #{@to}, #{@text}"
end
end
message = Message.new do
to "John"
from "Mary"
text "Awesome!"
deliver
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment