A number of blogs and tutorial have different indentations when they implement a case statement. Each have different indentation in reference to the 'when' lines. Is there a best practice for indenting when using a case loop?
Case statement in the Ruby is a way to execute different codes and expressions on the basis of the values provided to the case, case statement in Ruby is similar to basic switch cases in the other programming languages.
Case statement in the Ruby is a way to execute different codes and expressions on the basis of the values provided to the case, case statement in Ruby is similar to basic switch cases in the other programming languages. In case of the switch case in Ruby it has three main components case (it is similar to switch in general concept, it takes the ...
Note: In other programming languages this is known as a switch statement. The components of a case statement in Ruby: Starts a case statement definition. Takes the variable you are going to work with. Every condition that can be matched is one when statement. If nothing matches then do this.
As you can see, the condition is reversed because Ruby calls === on the object on the left. The === is just a method that can be implemented by any class. In this case, Range implements this method by returning true only if the value is found inside the range.
The consensus is to indent when
as the same level as case
.
case sym
when :foo then ...
when :bar then ...
else ...
end
I think this is well established, and have not seen any authentic source that claims otherwise.
This is the way I've seen it used. The 'what happens in the event of the 'when' should be on the next line and indented.
case something
when 'a'
what happens when 'a'
when 'b'
what happens when 'b'
when 'c'
what happens when 'c'
end
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