Posted over 8 years ago. Visible to the public. Repeats. Linked content.

Ruby blocks: Braces and do/end have different precedence

Braces and do/end are completely swappable—almost all the time. They have different precedence. It’s not often that anyone comes across what this means in practice.

TL;DR {} binds stronger than do…end (as always in Ruby, special characters bind stronger than words)

Demo

Right way

Copy
names = ['bRUce', 'STaN', 'JOlIE'] # Blocks in braces are passed to the rightmost method print names.map { |name| name.downcase } print(names.map do |name| name.downcase end) # equivalent => ["bruce", "stan", "jolie"]

Wrong way

Avoid the examples below, as you pass at least one block to print and not to the enumerator.

Copy
names = ['bRUce', 'STaN', 'JOlIE'] # Blocks in do…end are passed to the leftmost method print names.map do |name| name.downcase end print(names.map) { |name| name.downcase } # equivalent => #<Enumerator:0x00000001e793b0> # Combined print names.map { |name| name.downcase } do |name| name.capitalize end print(names.map { |name| name.downcase }) do |name| name.capitalize end # equivalent => ["bruce", "stan", "jolie"]

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Henning Koch
Last edit:
over 4 years ago
by Emanuel De
Keywords:
ruby, lambda, proc, block
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more