Skip to content

Instantly share code, notes, and snippets.

@momolog
Created July 10, 2013 14:58
Show Gist options
  • Select an option

  • Save momolog/5967027 to your computer and use it in GitHub Desktop.

Select an option

Save momolog/5967027 to your computer and use it in GitHub Desktop.
Different ways to bind procs, depending on ruby version and whether you work with ActiveSupport.
class A
LAMB1 = lambda{ do_it }
LAMB2 = proc{ do_it }
def do_it
puts "AAA"
end
end
class B
def do_it
puts "BBB"
end
def test_1
instance_eval(&A::LAMB1)
end
def test_2
instance_eval(&A::LAMB2)
end
def test_3
A::LAMB1.bind(self).call
end
end
b = B.new
begin
# will work in ruby 1.8.7 only
b.test_1
rescue Exception => e
puts e
end
begin
# will work in ruby 1.8.7 and 1.9.3
b.test_2
rescue Exception => e
puts e
end
begin
# will work IN RAILS in ruby 1.8.7 and 1.9.3.
b.test_3
rescue Exception => e
puts e
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment