Skip to content

Instantly share code, notes, and snippets.

View rauchy's full-sized avatar
💭
Redashing!

Omer Lachish rauchy

💭
Redashing!
View GitHub Profile
@rauchy
rauchy / clean_room.rb
Created November 26, 2012 04:18
Blocks In-depth: instance_eval
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)
@rauchy
rauchy / invocation_arity.rb
Created November 25, 2012 18:19
Blocks In-depth: Lambdas
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)
@rauchy
rauchy / ampersand.rb
Created November 25, 2012 11:33
Blocks In-depth: Procs
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}" }
@rauchy
rauchy / bypass_gates.rb
Created November 25, 2012 04:39
Blocks In-depth: Scope
a = 1
Foo = Class.new do
# a == 1 here
b = 2
define_method(:bar) do
# a == 1 here, b == 2 here
c = 3
end
@rauchy
rauchy / blocks_keep_binding.rb
Created November 24, 2012 13:50
Blocks In-depth: Closures
def greet
"Hello, #{yield}!"
end
name = "Omer"
greet { name } # => "Hello, Omer!"
@rauchy
rauchy / block_given.rb
Created November 20, 2012 10:27
Blocks In-depth: Calling Blocks
def log
if block_given?
puts "before block"
yield
puts "after block"
end
end
log # nothing will be displayed!
@rauchy
rauchy / right_user_spec.rb
Created March 16, 2012 12:08
Positive and Negative Assertions
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
factory :some_model do
factory :some_other_model do
end
end