Ruby Tips & Tricks #2: Lambda Literals in 1.9.3

Ruby 1.9.3 [support] is coming to an end and with it — an end of a syntax limitation. Welcome to the second Ruby Tips & Tricks post.

Have you ever wondered why the [community style guide] suggest you to write lambda literals like ->(a, b) { a + b } instead of -> (a, b) { a + b }? Well, there is a bit more than aesthetics to it…

In Ruby 1.9.3 -> (a, b) { a + b } isn’t syntactically valid:

>> -> (a, b) { a + b }
SyntaxError: (irb):1: syntax error, unexpected tLPAREN_ARG, expecting keyword_do_LAMBDA or tLAMBEG
-> (a, b) { a + b }
    ^
    from /1.9.3-p545/bin/irb:12:in `<main>'

The fore mentioned expression is valid in Ruby 2.0.0 and above. That’s what I call a significant whitespace!

Have you ever been bitten by it? Drop me a line on twitter at [@gsamokovarov].

[support]: https://www.ruby-lang.org/en/news/2014/01/10/ruby-1-9-3-will-end-on-2015/
[community style guide]: https://github.com/bbatsov/ruby-style-guide
[@gsamokovarov]: https://twitter.com/gsamokovarov

 
32
Kudos
 
32
Kudos

Now read this

Ruby Tips & Tricks #1: Procs with Empty Blocks

Hello and welcome to the first Ruby Tips & Tricks post. In this series I would talk about interesting bits in Ruby. Let’s start the series with a documented, but little known feature of the Proc.new constructor. When Proc.new is... Continue →