Ruby multiline block without do end

Viewed 19109

I’m a beginner in Ruby, so I’m sorry to ask something so simple, but is there anything wrong with this code –

3.upto(9) {
  print "Hello"
  puts " World"
}

or

3.upto(9) { |n|
  print "Hello "
  puts n
}

It works well enough, but most of the code samples I see use the syntax of

3.upto(9) do |n|
  print "Hello "
  puts n
end

is it just the convention to only use curly braces for single statements? Coming from C/C# the first seems more natural to me, but when in Rome!

3 Answers

There's seamless. From the README:

Python allows you to signal the end of a code block with indentation. Ruby suffers from an extremely verbose and tedious block terminator, "end". Much like Lisps end up with dozens of close-parens, Ruby files that use modules and classes heavily end up with a plethora of "ends" that just aren't necessary.

Write a Ruby file, but skip all the "ends". Line up your code blocks like in Python. Then just call it 'your_file.rbe', require 'seamless', and require 'your_file'. Seamless does the rest.

Should this ever see widespread use? I don't know. But it's pretty fun!

Related