RUBY (part-9)

RUBY (part-9)

Introduction

Hello readers this blog is all about some unique nameless things we can call. But after knowing their functionality I feel very bad by saying they are doing such amazing time-saving things but not even one person has the courtesy to name them. In this blog, I will make you understand the whole working of these cuties.

Block

Ruby blog is just a bit of code that can be executed.

let's take an example

print "Hello World!"

The question arises shall we call the above code a block? but could not agree more because if call that a block there is no meaning actual block.

The block can be represented by using curly braces { } or it starts with do and close with end keywords.

If you remember we covered this topic in the iterators. I am discussing this again here because there is some relation among all the things we are gonna discuss.

Let's try to code the actual block

l = [1,2,3,4,5]
l.each do |i|
    print i**2 #taking each element and print 2 power of that number
end
output:
1 4 9 16 25

In place of do and end, at that position, we can even use this --> { }

l = [1,2,3,4,5]
l.each {|i| print i**2}
output:
1 4 9 16 25

Yield

Yield is also a block of code that can be passed while calling the function. But we know that while calling the function we have only arguments and I am saying here we can even pass a block. Nothing wrong with that and both are different by their brackets.

arguments --> function_name( arguments)

block --> function_name{block}

This particular block executes where ever we use the yield keyword in the body of the function. Let's code this

def function
    yield
    puts "Hello World"
    yield
end
function { puts "This is because yield keyword in the body"}
output:
This is because yield keyword in the body
Hello World
This is because yield keyword in the body

In this way where ever we place yield it executes the block of code that we pass while calling the function.

Yield is just like the function we can pass the arguments, and return values.

def function 
    res= yield(2,3) #passing arguments, return the result to res variable
    print res
end
function{ |x,y| puts x + y}
output:
5

Proc (procedure object)

In simple words, we can think of proc as a saved block. A proc object is an encapsulation of a local variable, passed to a method or another method proc, and can be called.

It is an object that contains the code block.

Let's look at what the need is for saving our block and naming It as proc.

  1. As we are saving proc in the object meanwhile that particular has all the power and abilities of objects.

2. We are storing them in a single object they can be called over and over without rewriting.

Probably these are two main reasons.

Creation

There are many ways in ruby to do one task, in the same way we can create a proc in different ways.

proc1 = Proc.new #Proc class constructor
proc2 = proc { block } #kerner proc method

In the above code, proc1 is the object that stores the proc.

proc1 = proc{print "hello world"}
print proc1
output:
<Proc:0x1827359838975897c38>

So, if you the above code instead of printing the hello world it is showing us the respected address of the object. In order to print we have to use call method in the Proc class.

proc1 = proc{print "hello world"}
print proc1.call
output:
hello world

Calling procs on methods

As the proc is storing the result in a single object we can even use the object as a parameter in any function call.

Do remember one thing if you want to call the method using proc in the parameter place "&" in front of the proc.

def function 
    yield
end
proc1 = proc{print "hello world"}
function(&proc1)#calling a proc
output:
hello world

Lambda

This appears in python also and in python, we can call it an anonymous function. Except for 2 major differences lambda is exactly function the same as proc as we discussed before.

  1. Lambda checks the no.of arguments passed to it, while proc does not.

  2. when lambda returns it passes control back to the calling method when proc returns it does so immediately without going back to the calling method.4

Things get more clear when we look at them with the help of code.

def lambda_fun
    n = lambda { return "inside the lambda"}
    n.call
    return "This is executing because of lambda_fun"
end
def proc_fun
    n = proc { return "inside the proc"}
    n.call
    return "This is executing because of proc"
end
puts lambda_fun
puts proc_fun
output:
This is executing because of lambda_fun
inside the proc_fun

The above example code is the visual explanation of the second point.

QUICK REVIEW

  1. A block is just a bit of code between do...end or { } it's not an object on its own but it can be passed to methods like each, select, etc.,

  2. A proc is a saved block we can use over and over

  3. A lambda is just like a proc, only it cares about the no.of arguments it gets and it returns to its called method rather than returning immediately.

Conclusion

This is all about procedural programming in ruby. In the next blog, I will discuss a few object-oriented programming until then stay tuned and happy coding. A small note from my side all these are just overviews of how things work the real potential lies in how effectively you are using them in your problem-solving.

Did you find this article valuable?

Support Malavi Pande by becoming a sponsor. Any amount is appreciated!