Server Side Programming Articles - Page 690 of 2650

String reverse vs reverse! function in Ruby

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

524 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

340 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

How to 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 does Recursion work in Ruby Programming?

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

401 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

Yield keyword in Ruby Programming

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

508 Views

There are often cases where we would want to execute a normal expression multiple times inside a method but without having to repeat the same expression again and again. With the yield keyword, we can do the same.We can also pass arguments to the yield keyword and get values in return as well. Now let's explore some examples to see how the yield keyword works in Ruby.Example 1Consider the code shown below where we are declaring a normal yield keyword twice inside a method and then calling it.def tuts    puts "In the tuts method"    # using yield keyword ... Read More

True, False and Nil in Ruby Programming

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

348 Views

We know that everything in Ruby is treated as an object, and so the true, false and nil as well. They are built-in types that Ruby provides to do different conditional checks and more. In this article, we will explore different examples of the true, false and nil data types and how to use them.True, False in RubyLet's start with a very simple example where we will check if two variables are equal or not.Example 1Consider the code shown belowfirst = 10 second = 10 if first == second    # If Condition is true    puts "True! First ... Read More

Static Members in Ruby Programming

Mukul Latiyan
Updated on 25-Jan-2022 10:25:26

3K+ Views

Static Members in Ruby are declared with the help of the class. Since Ruby doesn't provide a reserved keyword such as static, when we make use of the class variable, then we create a static variable and then we can declare a method of that class in which the static variable is defined as a static method as well.In Ruby, there are two implementations for the static keyword −Static variableStatic methodIn this article, we will explore both these implementations where first, we will explore a code example of how to declare a static variable and then we will see how ... Read More

How to adjust the sharpness of an image in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:58:11

2K+ Views

To adjust the sharpness of an image, we apply adjust_sharpness(). It's one of the functional transforms provided by the torchvision.transforms module. adjust_sharpness() transformation accepts both PIL and tensor images.A tensor image is a PyTorch tensor with shape [C, H, W], where C is number of channels, H is image height, and W is image width. This transform also accepts a batch of tensor images. If the image is neither a PIL image nor tensor image, then we first convert it to a tensor image and then apply the adjust_sharpness(). The sharpness should be any non-negative number.Syntaxtorchvision.transforms.functional.adjust_sharpness(img, sharpness_factor)Parametersimg – Image of ... Read More

How to construct a complex tensor with the given real and imaginary parts in PyTorch?

Shahid Akhtar Khan
Updated on 25-Jan-2022 08:51:23

2K+ Views

With given real and imaginary parts, we can construct a complex number in PyTorch using torch.complex() method. The real and imaginary parts must be float or double. Both the real and imaginary parts must be of the same type. If the real part is float, then the imaginary must also be float.If the inputs are torch.float32, then the constructed complex tensor must be torch.complex64.If the inputs are torch.float64, then the complex tensor must be torch.complex128.Syntaxtorch.complex(real, imag)Parametersreal and imag − Real and imaginary parts of the complex tensor. Both must be of the same dtype, float or double only.StepsWe could use the ... Read More

Advertisements