Introduction
Assume we wish to return a string if a condition is satisfied. In Ruby, that if statement may look something like this:
if something is true == true
return result'
end
Rubocop, a Ruby code linter and general style guardian would provide a few suggestions for basic if statements like the one seen above. Let's not make things complex here itself. If as a Modifier in Ruby is very much similar to the Unless modifier, just the difference in condition.
So, Let’s begin this exciting journey of If as a Modifier in Ruby. Guys sit tight because if as a Modifier is a small topic but important for interviews,
If Modifier
When used in its usual statement form, Ruby's grammar requires it to be followed by the end keyword. This is quite problematic for basic, single-line conditionals. This is just a parsing issue, and the answer is to use the if keyword as the separator between the code to be performed and the conditional expression.
Rather than writing:
if expression then code end
we can simply write:
code if expression
When used in this manner, If is referred to as a statement (or expression) modifier. If you're a Perl programmer, you're probably familiar with this syntax. If not, please keep in mind that the code to run comes first, followed by the expression. As an example:
puts message if message # Output message, if it is defined
This syntax emphasizes the code to be performed rather than the circumstance under which it will be executed. When the condition is trivial or usually always true, using this approach can make your code more legible.
True.
Despite the fact that the condition is written last, it is assessed first. If it compares to anything, If the value is not false or nil, the code is executed, and its value is utilized as the return value. The updated expression's value Otherwise, the function is not run, and the return value is returned. The changed expression has no value. Obviously, this grammar does not permit any type of otherwise clause.
If as a modifier in Ruby, it must come directly after the changed statement or phrase, with no line breaks in between. In the above example, inserting a newline results in an unchanged method call followed by an incomplete if statement:
if message # Incomplete!
Puts message # Unconditional!
The if Modifier has far lower precedence than the assignment operator and binds more loosely. When you use it, be sure you understand what expression you're changing.
For instance, the following two lines of code differ:
y = x.invert if x.respond_to? :invert
y = (x.invert if x.respond_to? :invert)
The Modifier applies to the assignment phrase in the first line. If x does not have an inverted method, nothing happens, and the value of y is not changed.
The if Modifier only applies to the method call in the second line. If x lacks an inverted method, the changed expression evaluates to nil, and this is the value assigned to y.
Lets Understand it better with an example:-
condition1 = true
puts "condition1: if modifier result" if condition1
condition2 = false
puts "condition2: if modifier result" if condition2
Output:
condition1: if modifier result
No output from Condition2
Explanation:- If the condition is true, the if modifier runs code. If the condition is false, the modifier produces no output.
A single closest expression is bound by an if Modifier. If you wish to alter many expressions, use parentheses or a beginning statement to group them. However, this method is troublesome since readers do not realize the code is part of a conditional until they reach the bottom. Furthermore, utilizing an if Modifier in this manner sacrifices the consciousness that is the major value of this syntax. When more than one line of code is involved, a traditional if statement rather than an if modifier should be used.