Found 49 Articles for Ruby

Difference between 'include' and 'extend' in Ruby

Mukul Latiyan
Updated on 12-Apr-2022 08:49:45

1K+ Views

In Ruby, when we are using the include keyword, we are importing a module code, but we aren't allowed to access the methods of the imported modules with the class directly because it basically gets imported as a subclass for the superclass.On the other hand, when we are using the extend keyword in Ruby, we are importing the module code but the methods are imported as class methods. If we try to access the methods that we imported with the instance of the class, the compiler will throw an error.Now let's use these two keywords in a Ruby code to ... Read More

How to use the "or" keyword in Ruby?

Mukul Latiyan
Updated on 12-Apr-2022 08:45:00

1K+ Views

In Ruby, we use the "or" keyword to return the logical difference between its two operands. In simple terms, we can say that a condition becomes True if both the operands are true."or" returns True if any one of the conditions/expressions is "true".It returns False only when all the conditions are "false".It should be noted that the or keyword is equivalent to the "||" logical operator, but it has lower precedence in Ruby.SyntaxThe syntax of the or keyword is shown below.Condition1 or Condition2Let's use the or keyword in a Ruby code and see how it works.Example 1Consider the code shown ... Read More

How to use the "not" keyword in Ruby?

Mukul Latiyan
Updated on 12-Apr-2022 08:39:24

1K+ Views

In Ruby, we use the not keyword when we want to get an expression and then invert its Boolean value. In simple words, if an expression evaluates to True, then by using the not keyword, we will get False as the result of the expression.It can be said that the not keyword works like the "!" operator in Ruby, but the only difference between them is that the "!" operator has the highest precedence of all operators and the "not" operator has the lowest.SyntaxHere is the syntax of the not keyword in Rubynot expressionNow, let's take a couple of examples ... Read More

How to use the "defined?" keyword in Ruby?

Mukul Latiyan
Updated on 12-Apr-2022 08:37:13

595 Views

Ruby provides a special keyword known as defined? that is used to determine if a particular object or data type is defined in Ruby or not.The defined? keyword will return a string describing its expression or argument, if the passed expression or argument is defined. It returns nil if the expression or the argument is not defined in Ruby.SyntaxThe syntax of the defined keyword in Ruby is given belowdefined? variable_nameNow, let's take a couple of examples to demonstrate how to use the defined keyword in Ruby.Example 1Consider the code shown below.# Declare the Variables programming = 2 ruby = programming ... Read More

How to use the 'and' keyword in Ruby?

Mukul Latiyan
Updated on 12-Apr-2022 08:35:03

615 Views

'and' Keyword in RubyIn Ruby, we use the "and" keyword to return True if both the operands are true, and False if one or more of the operands is false. It should be noted that the and keyword is equivalent to the && logical operator, but it has lower precedence in Ruby.SyntaxThe syntax of the and keyword is shown below.expression1 and expression2Let's use the and keyword in a Ruby code and see how it works.ExampleConsider the code shown below.variable1 = "sunshine" variable2 = "$un$h1ne" # Using and keyword if (variable1 == "sunshine" and variable2 == "$un$h1ne")    puts "Learn ... Read More

How to invoke methods in Ruby?

Mukul Latiyan
Updated on 12-Apr-2022 08:32:45

628 Views

Methods in Ruby, as in other programming languages, bundle one or more repeatable statements into a single unit. Method names in Ruby should begin with a lowercase letter. If you begin a method name with an uppercase letter, Ruby might think that it is a constant and hence can parse the call incorrectly.Methods should be defined before calling them, otherwise Ruby will raise an exception for invoking an undefined method.There are different ways in which we can invoke methods in Ruby. When we are invoking a method in Ruby, the use of the parentheses is optional and hence they can ... Read More

Useful Methods from Numeric Class in Ruby

Mukul Latiyan
Updated on 12-Apr-2022 08:30:09

256 Views

There are several methods in Ruby that can be used on numbers. In this article, we will explore some of these useful methods and how to use them.number.even in RubyThis method is used when we want to check if a particular number is even or not. Consider the code shown below −ExampleConsider the code shown below.num = 14 puts num.even? num = 19 puts num.even?OutputIt will produce the following output.true falsenumber.odd in RubyThis method is used when we want to check if a particular number is odd or not. Consider the code shown below −ExampleConsider the code shown below.num ... Read More

Useful Methods in Float Class in Ruby

Mukul Latiyan
Updated on 12-Apr-2022 08:26:02

278 Views

The Float class in Ruby is a subclass of the Numeric class. Its objects are the representations of real numbers, using the native architecture representation of floating-point numbers.Let's consider the different methods that are available in the float class in Ruby.== Method in RubyThe == method is used when we want to return True, if two objects are equal.ExampleConsider the code shown below.puts 3.7 == 4 puts 3.7 == 3.7OutputIt will produce the following output.false trueabs Method in RubyThe abs method is used when we want to return the absolute value of a float.ExampleConsider the code shown below.puts (-50.56).abs puts ... Read More

How to Read and Writes Files in Ruby

Mukul Latiyan
Updated on 12-Apr-2022 08:22:01

5K+ Views

Ruby provides us with different methods that we can use to handle files. In simple terms, file handling involves different processes such as creating a new file, reading the contents present in a file, writing some content to a file, appending content to a file, deleting a file and more.There are different modes that one can use to do file handling in Ruby. These are −r = Read-only moder+ = Read-Write modew = Write-mode onlyw+ = Read-write modeAnd some more. These four mentioned modes are the most commonly used ones, when it comes to file handling in Ruby. In this article, ... Read More

How to Handle Exceptions in Ruby

Mukul Latiyan
Updated on 12-Apr-2022 08:05:57

166 Views

Exceptions in programming are errors that occur at runtime. They are hard to handle in an effective manner, but it is important to handle them, else they halt the program execution.Some of the common exceptions that one can encounter are −trying to open a file that is not there, dividing the number with zero, running out of memory etc.Now let's take a couple of examples to demonstrate how exceptions halt the execution of a Ruby program.Example 1Consider the code shown below.taking two integer value $First = 10; $Second = 0; # divide by zero error $Third = $First / ... Read More

Advertisements