For example, does the presence or absence of do
in the following code affect the behavior of the program at all?
while true do
puts "Hi"
break
end
while true
puts "Hi"
break
end
There are two ways of defining a block in Ruby: The first is using the do.. end keyword, the other is using a pair of curly braces. Do.. end block is mainly used when defining a block of code that spans multiple lines, while curly braces {} are used when defining a block of code that spans a single line.
Ruby's BEGIN and END blocks (in uppercase) are reserved keywords in Ruby and are pretty straightforward to use. They enable you to specify blocks of code that you want your program to run at the beginning and end of its execution, regardless of their position in the source file.
Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects or as constants. Doing this may result in compile-time error. Example: # Ruby program to illustrate Keywords.
Ruby Identifiers Identifiers are names of variables, constants, and methods. Ruby identifiers are case sensitive. It means Ram and RAM are two different identifiers in Ruby. Ruby identifier names may consist of alphanumeric characters and the underscore character ( _ ).
According to The Ruby Programming Language book Section 5.2.1:
The
do
keyword in awhile
oruntil
loop is like thethen
keyword in anif
statement: it may be omitted altogether as long as a newline (or semicolon) appears between the loop condition and the loop body.
So, no, it won't change the behavior, it's just optional syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With