Hey Ninja!! It's great to see you going through yet another informative blog. Ruby is a modern scripting language used by top corporations around the world.
If you have worked with languages such as C/C++ or Java, you must be familiar with loops, conditional statements, flow control statements, etc. In Ruby, you are provided with similar tools but with extra flexibility. Here, statements can be modified to have the body first followed by the conditions. In this blog, you will learn about Ruby's various statement modifiers and their implementation.
A statement is a single line( or multiple) code that tell the compiler to perform a specific task. Statements are used to assign values, print values or execute any other declarative commands. These are different from expressions as they do not return any value.
Example: If statement
a = if b > c 10 end puts(a) #=> 10
Modifiers
Statement modifiers allow adding conditions at the end of formal statements, which will be executed based on the requirement. These trailing conditionals are similar to guard clauses in mathematics or functional languages. The modifiers must be written with proper readability guidelines to avoid confusion.
Types of statement modifiers
'If': 'If' expression performs a task if a specific condition is true.
You can use 'if' to modify a statement. When used as a modifier left-hand side expression is the 'then' part, whereas the right-hand side expression is the 'test' condition.
puts "Coding Ninjas Ruby Blog." puts "If statement modifier." y = 10 y += 5if y.zero? print(y)
Output:
‘Unless’: It is the opposite of the 'if' statement, i.e. the expression is executed if the condition is false.
You can use ‘unless’ to modify a statement. When implemented as a modifier, the left-hand side expression is the 'then' part, whereas the right-hand side expression is the 'test' condition.
puts "Coding Ninjas Ruby Blog" puts "Unless statement modifier." y = 10 y += 5unless y.zero? print(y)
Output:
While: It executes a statement as long as it is true. The condition is separated from code using a reserved keyword 'do', a new line or a backslash. Similar to 'if' and 'unless', 'while' can also be used as modifiers.
#CodingNinjas #Ruby_Statement_Modifiers_While
puts "CodingNinjas" puts "Ruby_Statement_Modifiers_While" x = 0 x += 1while x < 10 puts 'Value of X at end: ' p x
Output:
Until: It executes a statement while it is false. 'Until' is the opposite of the 'while' statement. The condition is separated from the code using a reserved keyword 'do', a new line, a backslash or a semicolon.
#CodingNinjas #Ruby_Statement_Modifiers_Untill
puts "CodingNinjas" puts "Ruby_Statement_Modifiers_Untill" y = 0 p y+=5until y > 10
Output:
Rescue: It is used to handle exceptions but can be considered as a modifier. Its different from other modifiers because here, the exception status of the lefthand expression affects the execution of the righthand operand.
A raised exception can be rescued to prevent the app from crashing if it reaches the top of the call stack. When raise is used without specifying an exception class, Ruby will, by default, consider RuntimeError. Follow the example below:
#CodingNinjas #Ruby_Statement_Modifiers_Rescue
begin raise 'Hi Ninja , this is an intentional exception!!' rescue StandardError => e puts "Rescue error information : #{e.inspect}" end
Output:
Frequently Asked Questions
What is the advantage of using Ruby?
It is flexible and open-source with native multi-threading support. Changes are easy to make in Ruby. Also, it is exceptionally secure. The programming language holds and stores all the objects by reference instead of value, preventing any data from being hacked.
What is a scripting language?
It is a language for runtime systems that automates the execution of tasks that humans would otherwise execute manually. Scripting languages interpret at runtime instead of being compiled first.
What is an expression?
Any statement which returns something after the evaluation is called an expression. The purpose of expression is to create a new value. Description of expression varies among different programming languages.
What are control flow statements?
Flow statements that govern the code execution path are called control flow statements. Loops, conditionals, switch, goto etc., come under flow control.
How are comments written in Ruby?
Single-line comments are written using the ‘#’ symbol before the comment. ‘=begin’ and ‘=end’ surround comments in multiline commenting.
Conclusion
Well done, Ninja !! You have reached the end of this blog. We know it's hard to learn these complex concepts. You did a commendable job!!
In this blog, you learned about statement modifiers in Ruby. Various implementation aspects are covered in detail, and you now know about flow control, conditionals, rescue etc.
After reading about program execution in ruby, are you not feeling excited to read more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Ruby_Coding_Ninjas, Ruby on Rails, Coding Ninjas Studio, and Coding Ninjas guided paths.
You may follow official Ruby documentation, faqs and koans for further in-depth information.
Please do upvote our blogs if you find them helpful and informative!