In this tutorial, we'll learn about the methods in String class that help us to search and replace portions of the string based on a text or pattern. String.include?(string) - Returns true if str contains the given…
Text info can be read from varied sources and is often unsuitable for direct processing or usage by core functions. This necessitates methods for post-processing and data-fixing. In this tutorial, we'll learn how to…
In our encoding tutorial, we learned about the different ways Ruby 1.8 and Ruby 1.9 (and higher versions) represent strings internally. The major difference is a wide range of encoding (non-ascii) support in the later…
When you index a string, you extract or alter the portions. This is perhaps the most important operation you want to perform on strings. The string class provides a convenient array-like square bracket [] operator…
In Ruby, strings are objects of the String class, which defines a powerful set of operations and methods for manipulating text (e.g., indexing, searching, modifying, etc.). Here are a few easy ways to create…
Numbers, boolean values and strings are some of the fundamental data types that we have explored in our previous challenges. In this set of tutorials, we turn our attention to the data type referred to as String or…
Currying is a technique in which a function accepts parameters and turns it into a sequence of functions, each of them take 1 parameter. Example :- In the above example, lambda take two parameters , and return the…
In Partial Application, we create a lambda that takes a parameter and returns a lambda that does something with it. Example: In the above example, the lambda will take number as a parameter, and return a lambda. When…
Closure is a function/method that: ► Can be passed around like an object. It can be treated like a variable, which can be assigned to another variable, passed as an argument to a method. ► Remembers the value of…
Lambdas are anonymous functions. Lambdas in Ruby are objects of the class Proc. They are useful in most of the situations where you would use a proc. The simplest lambda takes no argument and returns nothing as shown…
Passing blocks is one way to pass functions as arguments to other functions. Blocks are one of the very few exceptions to the "everything is an object" rule in Ruby. Blocks are not objects, and they can't be saved to…
Higher order functions are one of the key components of functional programming. A higher order function is a tool that takes other functions as parameters or returns them as a result. Blocks are nameless methods that…
In our previous challenges, we explored some ways to pass arguments to our methods; however, they were limited in terms of the number of arguments we can pass to them. Using default parameters allows us to lower the…
In our previous challenges, we have been using methods (def method() .. endconstruct) to abstract compound operations, perform data manipulations and learn various concepts of the language, without talking in much…
In this challenge, we will show you ways in which we can add key-value pairs to Hash objects, delete keys from them, and retain them based on a logic. Consider the following Hash object: A new key-value pair can be…
You've seen the control structure each used on an array. Similarly, it is available for the Hash collection, as well. On Hash, it works in two ways. Consider the example Using each, each element can be iterated as or…
Hashes, also called associative arrays, are dictionary-like data structures which are similar to arrays. Instead of using integers to index an object, however, hashes use any object as its index. In this challenge, your…
The array class also allows to select and return a subset of an array based on some criteria defined in a block (a block is a group of code within {} that accepts a variable and returns a value). Selecting elements that…
The array class has various methods of removing elements from the array. Let's look at the array arr = [5, 6, 5, 4, 3, 1, 2, 5, 4, 3, 3, 3] Delete an element from the end of the array > arr.pop => 3 Delete an element…
Arrays provide a variety of methods that allow to add elements to them. push allows one to add an element to the end of the list. >x = [1,2] >x.push(3) => [1,2,3] insert allows one to add one or more elements starting…