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 MyLibrary | |
class Configuration | |
attr_accessor :protocol, :hostname, :port | |
end | |
attr_accessor :configuration | |
def configure(&block) | |
self.configuration ||= Configuration.new | |
self.configuration.instance_eval(&block) |
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
l = lambda { |first_name, last_name| puts "Hello, #{first_name} #{last_name}!" } | |
l.call("Omer", "Rauchwerger") # => Hello, Omer Rauchwerger! | |
l.call("Omer") # => ArgumentError: wrong number of arguments (1 for 2) | |
l.call("Omer", "Lachish", "Rauchwerger") # => ArgumentError: wrong number of arguments (3 for 2) |
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
def connect(address, &log) | |
log.call("About to connect…") | |
response = do_actual_connection(address) | |
log.call("Error: #{response.message}") if response.error? | |
end | |
connect("http://localhost") { |msg| puts "Info: #{msg}" } |
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
a = 1 | |
Foo = Class.new do | |
# a == 1 here | |
b = 2 | |
define_method(:bar) do | |
# a == 1 here, b == 2 here | |
c = 3 | |
end |
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
def greet | |
"Hello, #{yield}!" | |
end | |
name = "Omer" | |
greet { name } # => "Hello, Omer!" |
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
def log | |
if block_given? | |
puts "before block" | |
yield | |
puts "after block" | |
end | |
end | |
log # nothing will be displayed! |
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
describe User do | |
describe '#visit' do | |
it 'increases the visited attractions count after one visit' do | |
subject.visit('Rome') | |
subject.visited_attractions_count.should == 1 | |
end | |
it "doesn't increase the visited attractions count after several visits" do | |
5.times { subject.visit('Rome') } | |
subject.visited_attractions_count.should_not > 1 |
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
factory :some_model do | |
factory :some_other_model do | |
end | |
end |