Created
January 28, 2011 21:26
-
-
Save fnando/800989 to your computer and use it in GitHub Desktop.
Fluent interfaces + blocks example
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
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