Category: Basic

  • Ruby Break Statement

    The Ruby break statement is used to terminate a loop. It is mostly used in while loop where value is printed till the condition is true, then break statement terminates the loop. The break statement is called from inside the loop. Syntax: Example: Output: Ruby Next Statement The Ruby next statement is used to skip…

  • Ruby Comments

    Ruby comments are non executable lines in a program. These lines are ignored by the interpreter hence they don’t execute while execution of a program. They are written by a programmer to explain their code so that others who look at the code will understand it in a better way. Types of Ruby comments: Ruby…

  • Ruby Case Statement

    In Ruby, we use ‘case’ 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: Example: Output: Look at the above output, conditions are case sensitive. Hence, the output for ‘Saturday’ and ‘saturday’ are different.

  • Features of Ruby

    Ruby language has many features. Some of them are explained below:

  • Loops

    Loops in Ruby are used to execute the same block of code a specified number of times. This chapter details all the loop statements supported by Ruby. Ruby while Statement Syntax while conditional [do] code end Executes code while conditional is true. A while loop’s conditional is separated from code by the reserved word do, a newline, backslash \, or a semicolon ;. Example #!/usr/bin/ruby$i…

  • if…else, case, unless

    Ruby offers conditional structures that are pretty common to modern languages. Here, we will explain all the conditional statements and modifiers available in Ruby. Ruby if…else Statement Syntax if conditional [then] code… code…]… end if expressions are used for conditional execution. The values false and nil are false, and everything else are true. Notice Ruby uses elsif, not else if…

  • Comments

    Comments are lines of annotation within Ruby code that are ignored at runtime. A single line comment starts with # character and they extend from # to the end of the line as follows − #!/usr/bin/ruby -w# This is a single line comment. puts “Hello, Ruby!” When executed, the above program produces the following result…

  • Operators

    Ruby supports a rich set of operators, as you’d expect from a modern language. Most operators are actually method calls. For example, a + b is interpreted as a.+(b), where the + method in the object referred to by variable a is called with b as its argument. For each operator (+ – * / % ** & |…

  • Variables, Constants

    Variables are the memory locations, which hold any data to be used by any program. There are five types of variables supported by Ruby. You already have gone through a small description of these variables in the previous chapter as well. These five types of variables are explained in this chapter. Ruby Global Variables Global…

  • Classes and Objects

    Ruby is a perfect Object Oriented Programming Language. The features of the object-oriented programming language include − These features have been discussed in the chapter Object Oriented Ruby. An object-oriented program involves classes and objects. A class is the blueprint from which individual objects are created. In object-oriented terms, we say that your bicycle is an instance of…