Why Should You Avoid the Then Keyword in Ruby

Are return statements bad in Ruby?

"return" in ruby is only used if you are trying to return more than one value. e.g.

return val1, val2

or if it makes sense to return earlier from a function e.g.

#check if needed param is set
return if !param

#some operations which need param

which is easier than messing your code up with cascaded if-statements.

Conclusion: Use return every time it simplifies your code or it makes it easier to understand.

What is the difference between if statements with then at the end?

then is a delimiter to help Ruby identify the condition and the true-part of the expression.

if condition then true-part else false-part end

then is optional unless you want to write an if expression in one line. For an if-else-end spanning multiple lines the newline acts as a delimiter to split the conditional from the true-part

# can't use newline as delimiter, need keywords
puts if (val == 1) then '1' else 'Not 1' end

# can use newline as delimiter
puts if (val == 1)
'1'
else
'Not 1'
end

What is the meaning of the keyword do, in ruby?

do ... end (or alternatively { ... }) creates a so-called block, which is a type of anonymous function in Ruby. In your example that block is passed as an argument to group. group then does some bookkeeping to set the given groups as active, executes the block, and then deactivates the groups again.

Overriding the for keyword in Ruby. Is it possible?

You can't override the keyword itself, but one thing you can do is say what for does for your own classes. for calls each internally, so the following trick will work:

class MyFor
def initialize(iterable)
@iterable = iterable
end

def each
@iterable.each do |x|
puts "hello from for!"
yield x
end
end
end

# convenient constructor
module Kernel
def my(x)
MyFor.new(x)
end
end

for i in my(1..3)
puts "my for yielded #{x}"
end

When, if ever, to use the Ruby keyword for

behaves the same as

This is incorrect. for is built on top of each, but it is semantically distinct:

array = %w(a b c d)

array.each { |character| }
defined? character # nil

for character in array; end
defined? character # "local-variable"

The for keyword doesn't introduce a new scope. Any variables introduced inside the block remain visible outside of it; as if it was written inline.

You should take this fact into account when you decide which form to use.

Ruby - when I should use parenthesis or not when calling a function/method

There isn't an official standard for Ruby code best practices. However, a set of preferred styles has evolved in the Ruby community. It's a good idea to follow those preferred styles, just because it makes your code more easily readable by other Ruby programmers.

Nick Roz has given you a link to the style guide. I would also recommend that you consider installing and using rubocop. It will give you feedback on when and when not to parenthesize arguments, many other formatting matters such as proper indenting, and which of the often several different syntax options to choose in a particular situation.

To answer your specific question about whether or not to use parentheses for method arguments, the answer is yes, in general. Exceptions are, as the guide says, "methods that have 'keyword' status in Ruby." An example is puts (actually the Kernel.puts method). Most people don't use parentheses here, but the guide states that they are optional.

Another example, as Nick has said (although "methods with keyword arguments" isn't quite correct; in that case the arguments are symbols that represent methods rather than keywords), is attr_accessor, which is actually Module.attr_accessor.

So, as a general rule, if it looks like a "command" (a "keyword," if you will), but is actually a method, omit the parentheses. Otherwise, use them. And if you're not sure of the difference, get in the habit of using rubocop.

When is the do keyword required in Ruby?

According to The Ruby Programming Language book Section 5.2.1:

The do keyword in a while or until loop is like the then keyword in an
if 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.



Related Topics



Leave a reply



Submit