Found 47 Articles for Ruby

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

475 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

497 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

538 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

167 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

197 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

4K+ 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

107 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

Examples of String Functions in Ruby

Mukul Latiyan
Updated on 12-Apr-2022 07:56:32

240 Views

In this article, we will explore some useful string functions in Ruby that are widely used to format data.Get the Length of the StringTo find the length of the string, we will use the size method.ExampleConsider the code shown below.str = "TutorialsPoint" puts str.sizeOutputIt will produce the following output.14 To Check Empty stringTo check if a string is empty, we use the size method and comparison operator.ExampleConsider the code shown below.str = "TutorialsPoint" puts str.size == 0OutputIt will produce the following output.false To Extract a Substring from StringTo extract a substring from a string, we will use square bracket notation.ExampleConsider ... Read More

How Does Encapsulation Work in Ruby?

Mukul Latiyan
Updated on 12-Apr-2022 07:53:34

902 Views

Encapsulation is the ability to wrap the data into a single unit. In simple terms, it's a mechanism to wrap data and the code that manipulates the data. In Ruby, we can achieve encapsulation with the help of classes.Let's consider a very simple example where we will implement encapsulation.Example 1Consider the code shown belowclass Document    attr_accessor :name    def initialize(name)       @name = name    end    def set_name(name)       @name = name    end end d = Document.new('TP') d.set_name('TutorialsPoint') puts d.nameOutputIt will produce the following output −TutorialsPointExample 2Let's consider one ... Read More

Advertisements