Tonight's head-banging incident was directly related to lambda function syntax. Rather naively I assumed that what I thought would work would work. As always, assumption makes an ass out of umption.
This works as I would expect
This works as I would expect, too
However, the important thing to realize is that lambda definitions are whitespace sensitive, whereas a lot of the rest of the language does not seem to be.
Ruby really, really doesn't like this space:
So remember, lest you also spend some frustrated time going 'WTF' at the ruby interpreter. Lambdas which take method arguments are WHITESPACE SENSITIVE.
See This StackOverflow question (Spacing around parentheses in Ruby) for more information.
This works as I would expect
# a simple no-argument lambda
y = -> { 1 * 2 }
This works as I would expect, too
# a lambda that takes one argument and doubles it
z = ->(a) { a * 2}
puts z.(1)
=> 2
However, the important thing to realize is that lambda definitions are whitespace sensitive, whereas a lot of the rest of the language does not seem to be.
# v <-- note the space below the v
x = -> (a,b) { a * b * 3 }
Ruby really, really doesn't like this space:
C:\Ruby193\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/workspace/ruby-intro/rubymine-intro/modules/closure.rb
-e:1:in `load': C:/workspace/ruby-intro/rubymine-intro/modules/closure.rb:5: syntax error, unexpected tLPAREN_ARG, expecting keyword_do_LAMBDA or tLAMBEG (SyntaxError)
x = -> (a,b) { a * b * 3 }
^
from -e:1:in `<main>'
So remember, lest you also spend some frustrated time going 'WTF' at the ruby interpreter. Lambdas which take method arguments are WHITESPACE SENSITIVE.
See This StackOverflow question (Spacing around parentheses in Ruby) for more information.