Created
June 3, 2011 03:50
-
-
Save JoshCheek/1005840 to your computer and use it in GitHub Desktop.
Analyzing the different behaviours of Ruby's Proc
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
# My conclusion: | |
# lambda Use this if you want it to behave like a method. | |
# proc Avoid this, since its behaviour differs across commonly used versions. | |
# Proc.new Use this if you want it to behave like a block. | |
# -> This is just a 1.9 alternate syntax for lambda, so use it if you want method behaviour. | |
# use array, because 1.8.x doesn't have ordered hashes, which makes output confusing | |
proc_types = [ | |
['lambda' , method(:lambda) ], | |
['proc' , method(:proc) ], | |
['Proc.new' , Proc.method(:new) ], | |
] | |
puts '', `ruby -v` | |
proc_types.each do |name, method| | |
too_few_args = begin | |
method.call { |a, b| }.call(1) | |
"no error" | |
rescue | |
"error" | |
end | |
too_many_args = begin | |
method.call { |a, b| }.call(1, 2, 3) | |
"no error" | |
rescue | |
"error" | |
end | |
returns = begin | |
def meth(method) | |
method.call { return "from enclosing environment" }.call | |
"from block itself" | |
end | |
meth method | |
end | |
format = "%-10s (too few args: %-8s) (too many args: %-8s) (returns: %-10s)\n" | |
printf format , name, too_few_args, too_many_args, returns | |
end | |
# $ rvm proc_types.rb | |
# | |
# MacRuby 0.10 (ruby 1.9.2) [universal-darwin10.0, x86_64] | |
# lambda (too few args: error ) (too many args: error ) (returns: from block itself) | |
# proc (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# Proc.new (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# | |
# jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_24) [darwin-x86_64-java] | |
# lambda (too few args: error ) (too many args: error ) (returns: from block itself) | |
# proc (too few args: error ) (too many args: error ) (returns: from block itself) | |
# Proc.new (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# | |
# rubinius 1.1.0 (1.8.7 release 2010-09-23 JI) [x86_64-apple-darwin10.4.0] | |
# lambda (too few args: error ) (too many args: error ) (returns: from block itself) | |
# proc (too few args: error ) (too many args: error ) (returns: from block itself) | |
# Proc.new (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# | |
# ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-darwin10.3.0] | |
# lambda (too few args: error ) (too many args: error ) (returns: from block itself) | |
# proc (too few args: error ) (too many args: error ) (returns: from block itself) | |
# Proc.new (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# | |
# ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0] | |
# lambda (too few args: error ) (too many args: error ) (returns: from block itself) | |
# proc (too few args: error ) (too many args: error ) (returns: from block itself) | |
# Proc.new (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# | |
# ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10.4.0] | |
# lambda (too few args: error ) (too many args: error ) (returns: from block itself) | |
# proc (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# Proc.new (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# | |
# ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0] | |
# lambda (too few args: error ) (too many args: error ) (returns: from block itself) | |
# proc (too few args: no error) (too many args: no error) (returns: from enclosing environment) | |
# Proc.new (too few args: no error) (too many args: no error) (returns: from enclosing environment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My Conclusion
lambda: Use this if you want it to behave like a method.
proc: Avoid this, since its behaviour differs across commonly used versions.
Proc.new: Use this if you want it to behave like a block.
-> This is just a 1.9 alternate syntax for lambda, so use it if you want method behaviour.
Started a discussion about this on ruby-talk.