Skip to content

Instantly share code, notes, and snippets.

@alexrothenberg
Created March 6, 2014 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexrothenberg/9393607 to your computer and use it in GitHub Desktop.
Save alexrothenberg/9393607 to your computer and use it in GitHub Desktop.
MRI/JRuby/Rubinius discrepency
MRI - detect with a lambda complains about 2 parameters but map works
$ irb
2.1.1 :001 > lambda = ->(word, index) { word.length == 3 }
=> #<Proc:0x007fb52d00ab38@(irb):1 (lambda)>
2.1.1 :002 > %w(Hi there how are you).each_with_index.detect &lambda
ArgumentError: wrong number of arguments (1 for 2)
from (irb):1:in `block in irb_binding'
from (irb):2:in `each'
from (irb):2:in `each_with_index'
from (irb):2:in `each'
from (irb):2:in `detect'
from (irb):2
from /Users/alex/.rvm/rubies/ruby-2.1.1/bin/irb:11:in `<main>'
2.1.1 :003 > %w(Hi there how are you).each_with_index.map &lambda
=> [false, false, true, true, true]
JRuby - detect and map both work
$ jirb
jruby-1.7.10 :001 > lambda = ->(word, index) { word.length == 3 }
=> #<Proc:0x46c95759@(irb):1 (lambda)>
jruby-1.7.10 :002 > %w(Hi there how are you).each_with_index.detect &lambda
=> ["how", 2]
jruby-1.7.10 :003 > %w(Hi there how are you).each_with_index.map &lambda
=> [false, false, true, true, true]
Rubinius - detect and map both work
$ irb
rbx-2.2.3 :001 > lambda = ->(word, index) { word.length == 3 }
=> #<Proc:0x2c8c@(irb):1 (lambda)>
rbx-2.2.3 :002 > %w(Hi there how are you).each_with_index.detect &lambda
=> ["how", 2]
rbx-2.2.3 :003 > %w(Hi there how are you).each_with_index.map &lambda
=> [false, false, true, true, true]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment