Mukul Latiyan

Mukul Latiyan

363 Articles Published

Articles by Mukul Latiyan

Page 21 of 37

How Does Encapsulation Work in Ruby?

Mukul Latiyan
Mukul Latiyan
Updated on 12-Apr-2022 1K+ Views

Encapsulation is the ability to wrap the data into a single unit. In simple terms, it's a mechanism to wrap data and the code that manipulates the data. In Ruby, we can achieve encapsulation with the help of classes.Let's consider a very simple example where we will implement encapsulation.Example 1Consider the code shown belowclass Document    attr_accessor :name    def initialize(name)       @name = name    end    def set_name(name)       @name = name    end end d = Document.new('TP') d.set_name('TutorialsPoint') puts d.nameOutputIt will produce the following output −TutorialsPointExample 2Let's consider one ...

Read More

How to implement Data Abstraction in Ruby?

Mukul Latiyan
Mukul Latiyan
Updated on 12-Apr-2022 1K+ 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
Mukul Latiyan
Updated on 12-Apr-2022 291 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
Mukul Latiyan
Updated on 12-Apr-2022 2K+ 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
Mukul Latiyan
Updated on 12-Apr-2022 1K+ 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
Mukul Latiyan
Updated on 12-Apr-2022 812 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

How to handle errors within WaitGroups in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 22-Feb-2022 4K+ Views

There are chances that we might get some panic while running multiple goroutines. To deal with such a scenario, we can use a combination of channel and waitgroups to handle the error successfully and not to exit the process.Let's suppose there's a function that when invoked returns a panic, which will automatically kill the execution of the program, as when panic gets called it internally calls os.Exit() function. We want to make sure that this panic doesn't close the program, and for that, we will create a channel that will store the error and then we can use that later ...

Read More

How to wait for a goroutine to finish in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 22-Feb-2022 9K+ Views

We know that goroutines can be a bit tricky at first, and often we find cases where the main goroutines will exit without giving a chance to the inside goroutines to execute.In order to be able to run the goroutines until the finish, we can either make use of a channel that will act as a blocker or we can use waitGroups that Go's sync package provides us with.Let's first explore a case where we have a single goroutines that we want to finish and then do some other work.Example 1Consider the code shown below.package main import (    "fmt" ...

Read More

How to concatenate two slices in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 22-Feb-2022 3K+ Views

Whenever we talk about appending elements to a slice, we know that we need to use the append() function that takes a slice as the first argument and the values that we want to append as the next argument.The syntax looks something like this.sl = append(sl, 1)Instead of appending a single number to the slice "sl", we can append multiple values in the same command as well.Consider the snippet shown below.sl = append(sl, 1, 2, 3, 4)The above code will work fine in Go.When it comes to appending a slice to another slice, we need to use the variadic function ...

Read More

Sorting in Golang with sort Package

Mukul Latiyan
Mukul Latiyan
Updated on 22-Feb-2022 678 Views

The standard library of Golang provides a package that we can use if we want to sort arrays, slices, or even custom types. In this article, we will discover three main functions that we can use if we want to sort a slice in Golang. We will also see how we can create a custom sort function and custom comparator.Let's first check how we can sort a slice of integer, float64 and string values.ExampleConsider the code shown below.package main import (    "fmt"    "sort" ) func main() {    integerSlice := []int{3, 2, 14, 9, 11}    sort.Ints(integerSlice)   ...

Read More
Showing 201–210 of 363 articles
« Prev 1 19 20 21 22 23 37 Next »
Advertisements