Hacker News new | ask | show | jobs
by halostatue 5193 days ago
The suggested distinction elaborated on between {} and do/end blocks is…unfortunate.

The block types have different binding precedence, which affects their suitability for fluent (read as "DSL") interfaces. The reason that Rake tasks are defined as:

    task :foo do
      …
    end
and not

    task :foo { … }
is not just multi-line readability, it's because {} binds to :foo, but do/end binds to task.

Inasmuch as you want to make a different distinction between {} and do/end, Jim Weirich's suggested distinction is as good as any: use {} when you're going to be using the result of the block and do/end when you're not. Therefore:

    squared = (1..10).map { |i| i * i }
    (1..10).each do |i|
      puts i * i
    end
Yeah; I'll probably use {} for both because it's more readable in the simple case, but I find that I rarely use do/end when I'm using the value of the block, and I'll almost always use {}.