Control Structures - case Statements in Ruby

Control Structures – case Statements in Ruby

As we have seen in the previous lessons, we can check for multiple conditions using the if/elsif/else expression. A more simplified and flexible option is the case expression, which tests a value in when statements. You can have as many when statements as you need for a single case. Note that the case expression must be closed with the end keyword. case statements are used when you need to make different decisions based on a value.For example: Multiple values can be tested within a single when by separating the values with commas.For example: If you forget to put a comma between two values, Ruby will return a syntax error. syntax error, unexpected tINTEGER, expecting keyword_then or ‘,’ or ‘;’ or …

Read More
Logical Operators – and (&&), or (||), and not (!) in Ruby

Logical Operators – and (&&), or (||), and not (!) in Ruby

Logical operators are used to form more complex criteria that test more than one condition for an if expression. Today in this article we will discuss the logical operators used in Ruby language. Ruby has three logical operators: and (&&), or (||), and not (!). Ruby can use words instead of the logical operator symbols (and, or, not), but those have lower precedence and are generally avoided. and (&&) A conditional using and evaluates as true if, and only if, all of its operands are true. Otherwise, the entire conditional evaluates to false.For example:

or (||) The or (||) operator evaluates to true if either (or all) of its operands are true, and false if all operands are false.For …

Read More
Control Structures – unless Statement in Ruby

Control Structures – unless Statement in Ruby

Control Structure statement is the statement that controls the working flow of any program depending on its condition. Today in this article we will discuss one of the Basic Control Statement of Ruby i.e. unless unless Statements The unless expression is the opposite of an if expression. It executes the line of codes when a conditional is false. You can use an else block with the unless, just like you did with the if expression. The end keyword is also required to close the block.For example: The unless modifiers can also be used to execute code.For example:

Read More
Control Structures – If, elsif, else Statements in Ruby

Control Structures – if, elsif, else Statements in Ruby

Control Structure statement is the statement that controls the working flow of any program depending on its condition. Today in this article we will discuss the Basic Control Statements of Ruby i.e. if, elsif, else. if Statements You can use an if expression to execute the lines of code when a certain condition met. Each if has to have a corresponding end.For Example: The if modifiers can also be used to execute code.For example: elsif Statements The elsif (short form of else if) block is useful when you want to test multiple if conditions.A series of if elsif expressions can have a final else block, which is called if none of the if or elsif expressions are true. When an …

Read More
User Input Ruby

Getting User Input in Ruby

To get input from the user in Ruby, you can use the gets method, which returns what the user types as a string. To store the input for later use, you can assign the return value to a variable.

gets gets a line of text, including the new line at the end. If you do not want to include the new line, use the gets.chomp method.

The value of the input is a string. To convert it to an integer, you can use the gets.to_i method.

Read More
Strings in Ruby

Strings in Ruby

A string is a text between single or double quotation marks. However, some characters can’t be directly included in a string. For instance, single quotes can’t be directly included in a single quote string, because this would designate the end of the string. Characters like these can be included in a string by using an escape sequence, which is indicated by a backslash (\):

A string formed with double quotation marks can also include the \n escape sequence, which represents a new line.

String Interpolation You can embed any Ruby expression inside a double quote string using #{ }, Just as you did with variable names. Ruby evaluates the placeholders and replaces them with values:

Note that there …

Read More
Operators in Ruby

Operators in Ruby

Arithmetic Operators Exponent Operator Shorthand Assignment Operators / Self-Assignment Operators Parallel Assignment Arithmetic Operators in Ruby Math is an important part of programming. Ruby supports the following arithmetic operators: Addition Subtraction Multiplication Division Modulus – The modulus operator, represented by the percentage symbol (%), represents the remainder of a division operation.

When you divide two integer values, the result will be an integer, as shown in the above example. If you want to have a floating point result, one operand must be a floating point value:

Exponent Operator The ** represents the exponent operator for raising a number to a power to perform exponentiation.

Shorthand Assignment Operators All of the arithmetic operators have corresponding shorthand forms for assignment. …

Read More
Variables in Ruby

Variables in Ruby

Ruby has the following types of variables. Basic Variables Constants Variables Basic Variables Ruby is very simple. You do not need to declare the type of variable. Just assign the value to a variable with = sign. We can change the Basic variable’s values at any time with any value.

It is good practice to use the same data type in a single variable all over your application. It helps you to avoid mistakes. Constants Variable in Ruby Variables beginning with a capital letter are called constants. The value of a constant variable cannot be changed once it has been assigned.

Read More
Type of Comments in Ruby

Type of Comments in Ruby

Comments are lines of annotation within Ruby code that are ignored/bypassed at program runtime.  As other programming languages Ruby also has 2 types of comments. Single Line Comment Multi Lines Comment Single Line Comment In Ruby, the hashtag symbol is used to create a single-line comment.

Multi-line Comment Ruby also has the multi-line commenting option. Everything written between the =begin and =end is considered as comments and will be ignored at program runtime.

Read More
Hello World Ruby

Hello World Initial Program in Ruby

Hello World Program It’s a kind of tradition/custom 😀 for programmers to print the “Hello World” string in every new language. But it’s not a necessity to print this string only. 😉

Here, puts is a predefined function of Ruby language that will print the given string on screen with the newline at the end of string. puts function auto adds newline to each string where print function prints string to single line. All text values (strings) must be enclosed in single or double quotes.

Read More