10 ms·
A key difference in Ruby is that while blocks are conceptually just another argument, they aren't part of the syntactical argument list. This: strings.map {
by lobster_johnson 11y ago
A key difference in Ruby is that while blocks are conceptually just another argument, they aren't part of the syntactical argument list. This:
strings.map { |s| s.upcase }
is actually:
strings.map() { |s| s.upcase }
...and this is what it's not:
strings.map({ |s| s.upcase }) # Invalid
It's subtle, but it does make for a simpler, more readable syntax that lends itself to DSLs, since no parantheses are needed:
validate :name { |name|
errors.add("Bad name") unless name
}
It's feasible in other languages, of course; Elixir, for example, introduces a Ruby-like "do" block syntax which is more regular than Ruby's. It might be too late for JavaScript, but I'm no parser expert.