Found 47 Articles for Ruby

How to implement Data Abstraction in Ruby?

Mukul Latiyan
Updated on 12-Apr-2022 07:48:08

797 Views

Abstraction is an object-oriented programming concept, where the essential attributes of something are shown and all the unnecessary ones are hidden. With this approach, one can hide the implementation details and provide only the important interface.A very good example of abstraction is your car. It's a great example of abstraction. When you start the car by turning the key or pressing the start button, you don't necessarily need to know how the engine works or how it gets started, also what components are present in that car or what else is there. These details that are irrelevant to the driver ... Read More

Control flow alterations in Ruby

Mukul Latiyan
Updated on 12-Apr-2022 07:43:48

171 Views

In addition to loops, conditionals, and iterators, Ruby has some statements that are used to change the control flow in a program. In other words, these statements are pieces of code that execute one after the other until a condition is met.In this article, we will explore the following control flow alterations in Ruby −break statementnext statementredo statementretry statementLet's consider each of these one by one.break statementWhen a condition is True in Ruby, the break statement terminates a loop.ExampleConsider the code shown below.# break statement example itr = 1 while true    if itr * 6 >= 35   ... Read More

How to use the 'break' and 'next' statements in Ruby?

Mukul Latiyan
Updated on 12-Apr-2022 07:19:38

1K+ Views

break Statement in RubyIn Ruby, we use the break statement in order to make sure that we exit a certain loop after a condition. For example, suppose we want to print the numbers from 1 to 10, but once we hit the number 5, we just don't want the loop to print any numbers that come after it. In such a case, we can use the break statement.Example 1Let's take an example and understand how the break statement works in Ruby. Consider the code shown below.# break statement in Ruby #!/usr/bin/ruby -w itr = 1 # while Loop ... Read More

Array slice function in Ruby

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

950 Views

Sometimes we may want to exact a portion from an array data and perform some operation on it. In Ruby, we can do that with the help of the slice() function that takes two arguments, both of them indices, that are used to define a subsequence which then can be extracted from the array.SyntaxThe syntax of the slice() function is shown below −res = Array.slice(x, y)Here, x and y denote the starting index and the ending index, respectively.Example 1Now that we know a little about the slice() function on arrays, let's take a couple of examples and see how to ... Read More

Array shift function in Ruby

Mukul Latiyan
Updated on 12-Apr-2022 07:12:40

448 Views

Sometimes we need to extract a portion of an array data and perform some operation on the extracted data. In Ruby, we can perform such operations with the help of the shift() function.The shift() function takes one argument, which is an index that is used to remove the first element from that index and return all the elements before it. If the index is somehow invalid, then it returns nil.SyntaxThe syntax of the shift() function is shown below −res = Array.shift(x)Here, the argument "x" denotes the starting index.Example 1Now that we know a little about the shift() function on arrays, ... Read More

Lambda Functions in Ruby

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

307 Views

In Ruby, we can take the help of lambda functions when we want to use anonymous functions. They are also treated like objects in Ruby, as everything in Ruby is treated as objects.SyntaxThe syntax of declaring a lambda function is shown below.lambda = lambda {}Or, we can also make use of lambda literal.lambda = ->() {} Let's first check the type of the lambda functions in Ruby with the help of a program.Example 1Consider the code shown belowsome_lambda_function = lambda { puts "Welcome to TutorialsPoint!"} puts some_lambda_function.classOutputProc Example 2Now let's create another program where we will use our own ... Read More

Comparable module in Ruby

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

326 Views

In Ruby, the class whose objects can be ordered uses the Comparable mixin. Class definitions need to include an operator to compare receivers with each other. The operator will return either -1, 0, or 1.It returns -1 if the receiver is less than another object.If it is greater than another object, then it returns 1.It returns 0 if the receiver is equal to another object.In the Comparable module, the operator is used to implement the conventional comparison operators (*, =, and >) and sometimes also between? method as well.Now that we know a little about the comparable module in ... Read More

Array reverse() vs reverse! in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:46:03

706 Views

In this article, we will explore the two most widely used methods on arrays in Ruby. These are the reverse() method and the reverse! method.reverse() methodThe reverse() method in Ruby reverses the content of the array and returns a new array. Now, let's take a couple of examples to understand how it works.Example 1# reverse() method in Ruby # array declaration first_arr = [18, 22, 33, nil, 7, 6] # array declaration second_arr = [1, 5, 1, 3, 88, 9] # array declaration third_arr = [18, 22, 55, 6] # reverse method example puts "reversed array ... Read More

How does Inheritance work in Ruby?

Mukul Latiyan
Updated on 25-Jan-2022 11:42:32

1K+ Views

Inheritance is a key aspect of any OOP language. With the help of inheritance, we can reuse the methods that are defined on the parent class (also known as superclass) in the child class (also known as subclass).In Ruby, single class inheritance is supported, which means that one class can inherit from the other class, but it can't inherit from two super classes. In order to achieve multiple inheritance, Ruby provides something called mixins that one can make use of.Inheritance helps in improving the code reusability, as the developer won't have to create the same method again that has already ... Read More

Types of iterators in Ruby

Mukul Latiyan
Updated on 25-Jan-2022 11:38:00

107 Views

In Ruby, we have multiple types of iterators available to us. We will learn about the most common ones in this article, one by one.Each IteratorUsing this iterator, you can iterate over an array or a hash, returning each element as it is returned.Example 1Consider the code shown below# each iterator example (0..10).each do |itr|    puts itr endOutput0 1 2 3 4 5 6 7 8 9 10Times IteratorThis iterator implants a loop with a specific number of iterations. The loop starts from zero and runs until it gets one less than the specified number.Example 2# time iterator example ... Read More

Advertisements