Programming Articles - Page 816 of 3363

How to freeze objects in Ruby?

Mukul Latiyan
Updated on 25-Jan-2022 11:31:28

443 Views

Sometimes a situation arises where we would want to freeze the object instance so that it cannot be instantiated or modified and in Ruby, we can do that with the help of the freeze keyword.The approach is to invoke the Object#freeze statement.When we freeze an object, we are basically turning it into a constant and it should be noted that once an object is frozen, cannot be unfrozen.SyntaxThe syntax to freeze an object is shown below.Object.freezeNow that we know a little about freeze, let's take a couple of example to understand how it works.Example 1Consider the code shown belowveggies = ... Read More

Find subarray with given sum - (Nonnegative Numbers) in C++

sudhir sharma
Updated on 25-Jan-2022 12:52:30

457 Views

In this problem, we are given an array arr[] consisting of N positive integers stored in unsorted order. Our task is to find a subarray with a given sum.Let's take an example to understand the problem, Input : arr[] = {2, 5, 1, 4, 6, 9, 5} sum = 11 Output : subarray = {1, 4, 6}Explanation −Subarray sum = 1 + 4 + 6 = 11Solution ApproachA simple solution to the problem is using nested loops. We will loop through the array and using an inner loop, we will find subarray. For each subarray we will find the sum ... Read More

Range class methods in Ruby

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

280 Views

Range is a class in Ruby. Ruby ranges represent a set of values that have a beginning and an end. A range can be represented as a number, character, string, or object. A range is constructed with start_point...end_point, start_point...endpoint literals, or with ::new. It provides flexibility and reduces the size of the code.There are different methods available to us in the range class methods; some of these are class methods whereas some are instance methods. In this article, we will explore both the class methods and the instance methods as well.The only class method available is the .new one.new MethodThe ... Read More

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

How to 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

Find sub-string with given power in C++

sudhir sharma
Updated on 25-Jan-2022 12:06:06

302 Views

In this problem, we are given a string str and an integer pow. Our task is to find a sub-string with given power.We need to return the substring whose power is equal to pow.Power of string is the sum of powers of its characters.Power of character : a -> 1, b -> 2, c -> 3, ...Let's take an example to understand the problem, Input : string = "programming" power = 49 Output : 'pro'Explanation −Power of matrix : pro, power(p) = 16 power(p) = 18 power(p) = 15 Total = 16 + 18 + 15 = 49Solution ApproachA simple ... Read More

Transpose() function in Ruby Programming

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

425 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

550 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

How to push and pop elements in a queue in Ruby?

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

368 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

Advertisements