Created
July 10, 2013 14:58
-
-
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.
This file contains hidden or 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 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