Unless Statement and Unless Modifier in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:18:14

2K+ Views

Unless StatementWe know that we can use the if statement when we want to run some code based on a condition that evaluates to True. Ruby also provides a special statement known as unless statement that can be used to run some code based on the condition that evaluates to False.It is quite the opposite of the if statement.SyntaxThe syntax of the unless statement is shown below.unless condition    # some code else    # some code endNow let's take a couple of examples to demonstrate how it works.Example 1Consider the code shown below# unless statement example num = 2 ... Read More

Find Sub-Matrix with the Given Sum in C++

sudhir sharma
Updated on 25-Jan-2022 11:16:54

1K+ Views

In this problem, we are given a 2D matrix of size N*N and two variables sum and size. Our task is to find a sub-matrix with the given sum.We need to find a sub-matrix of size*size with element sum equal to sum.Let's take an example to understand the problem, Input : mat[][] = {    {1, 5, 7, 9}    {2, 4, 6, 8}    {1, 2, 5, 6}    {3, 6, 9, 3} } sum = 22 Size = 2 Output : YESExplanation −The submatrix of size k with sum 22 is {5, 7} {4, 6}Solution ApproachA simple solution ... Read More

Use BigDecimal in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:15:52

1K+ Views

Using BigDecimal, you can perform floating point decimal arithmetic with arbitrary precision. Let's try to understand the BigDecimal usecase with the help of an example. We will take two examples, where the first one will make use of no BigDecimal and in the second example, we will use BigDecimal.Consider the code shown below, where we aren't making use of BigDecimal and adding some decimal values multiple times to a number.Example 1# without using bigInteger def calculateSum()    sumOfNumbers = 0    10_000.times do       sumOfNumbers = sumOfNumbers + 0.0001    end    return sumOfNumbers end puts ... Read More

Transpose Function in Ruby Programming

Mukul Latiyan
Updated on 25-Jan-2022 11:12:23

398 Views

The transpose function in Ruby is mainly used to return the transpose of an array or a matrix.Syntaxarray.transposeOrMatrix.transposeLet's take a couple of examples of the transpose function on array first and then on matrices.Example 1Consider the code shown below# transpose() in array # array declaration first_arr = [[18, 22], [33, 3], [8, 6]] # array declaration second_arr = [[1, 3, 2, 5, 88, 9]] # print statements puts "transpose() output : #{first_arr.transpose()}" puts "transpose() output : #{second_arr.transpose()}"Outputtranspose() output : [[18, 33, 8], [22, 3, 6]] transpose() output : [[1], [3], [2], [5], [88], [9]]Example 2# transpose() in ... Read More

Array Push, Pop and Clear Functions in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:07:49

1K+ Views

The most widely used functions in Ruby when it comes to arrays are push(), pop(), and clear() functions. These functions are used when we want to enter, take out and clear the array data, respectively. In this article, we will learn about all these functions one by one.push() FunctionThe push function in Ruby is used to push elements at the end of an array. The function can accept single as well as multiple objects as arguments.Consider the code shown below as reference of the push() function.Example 1# push() function example in Ruby # few arrays first_arr = ["Letters", "a", ... Read More

String Reverse vs Reverse Function in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:02:01

517 Views

In Ruby, we have two functions available to us in case we want to reverse the contents of a string. These two functions are reverse and reverse!. While both of them are used to reverse the string, the only difference between them is that the reverse function reverses the string and then generates a new string, whereas the reverse! function reverses a string in place.reverse FunctionThe syntax of the reverse function is shown belownew_str = str.reverseNow, let's first look at an example of the reverse function in Ruby.Consider the code shown below.Example 1# the reverse method in Ruby str ... Read More

Push and Pop Elements in a Queue in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 10:55:33

330 Views

In this article, we will learn how to push (insert) and pop (remove) elements in a queue in Ruby.In Ruby, we can insert elements into a queue with the help of the push function. The syntax of the push function is shown below.queueName.push(element)Notice that the above syntax accepts one argument, which is the element that you want to insert into the queue. The result of the above command is that a new element will be added into the front of the queue.To remove an element from the queue, we can make use of the pop function. The syntax of the ... Read More

Hash Select and Select Methods in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 10:46:50

2K+ Views

In Ruby, we make use of the select() method when we want to find the array from the hash based on the condition and we make use of the select!() method when we want to check whether the array from the hash is present or not.Let's explore a few examples of the hash select() method to understand it better.Example 1# Hash.select() method # Hash value first = { "first" => 150, "second" => 200 } # Hash value second = {"first" => 150} # Hash value third = {"first" => 150, "third" => 300, "second" => 200} ... Read More

Use Global Variables in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 10:42:44

4K+ Views

Global variables have a global scope and they can be accessed from anywhere in a program. Assignments to global variables can be made from anywhere in the program. Global variables are always prefixed with a dollar sign.It is necessary to define a global variable to have a variable that is available across classes. When a global variable is uninitialized, it has no value by default and its use is nil.Now let's make use of the global variable in an example to understand it better. Consider the code shown below.Example 1# Global Variable example # global variable $global_var = 15 ... Read More

How Recursion Works in Ruby Programming

Mukul Latiyan
Updated on 25-Jan-2022 10:39:32

392 Views

A function that calls itself directly or indirectly is called a recursive function, and the corresponding function is referred to as a recursive function. Recursion makes the process easier and it definitely reduces the compilation time.We will try to understand the concept of recursion in Ruby with the help of a very simple example.Let's suppose we are given an array and we want to print the product of all the elements of the array, in order to do that, we have two choices, we can do it iteratively or we can do it recursively.Example 1Let's first do it iteratively. Consider ... Read More

Advertisements