While loop and do while loop in Ruby

The Ruby while loop is used to iterate a program several times. If the number of iterations is not fixed for a program, while loop is used. Ruby while loop executes a condition while a condition is true. Once the condition becomes false, while loop stops its execution. Syntax: Example: Output: Do…while loop: The Ruby […]

For loop in Ruby

Ruby for loop iterates over a specific range of numbers. Hence, for loop is used if a program has fixed number of iterations. Ruby for loop will execute once for each element in expression. Syntax: Ruby for loop using range: Output: Ruby for loop using array: Example: Output: Hope you guys found this page useful […]

How to write case statement in Ruby?

In Ruby, Use ‘case‘ keyword instead of ‘switch‘ and ‘when‘ instead of ‘case‘. The case statement matches one statement with multiple conditions just like a switch statement in other languages. Syntax: Let’s understand, With Example: Output: Hope you understand how to write case statement program 🙂

How to write if-else statement in Ruby?

You can use this statement in various format. like, if statement: Execute if block if condition is true. Check syntax here: See this example, Output: if-else statement: Execute if block if condition is true. Otherwise, execute else block. Check syntax here, See this example, Output: elsif statement: Execute if block if condition is true. Otherwise, […]

Variable declaration in Ruby

We can describe the variable in 4 different types : Local variable Class variable Instance variable Global variable Let’s check, How variable works in their comfort zones. Local Variable: Always start with lowercase or underscore ( _ ). It works only in the variable declared block. For example, Class Variable: Always start with @@ and […]

First Ruby Program

Before you start your first ruby program. Must sure that, You installed it. If you don’t know how to install ruby, Please refer this link for a proper guideline : Link Now, Open any text editor and write this program to check the first ruby program: Save this file as “first.rb” Note: Save Ruby program […]