Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 28, 2021 01:08 pm GMT

Sharpen your Ruby: Procs and Lambdas

I develop in Javascript, Python, PHP and Ruby. By far Ruby is my favorite programming language.

Together let start a journey and revisit our Ruby foundations.

Each post will include some theory but also exercise and solution.

If you have any questions/comments or your are new and need help, you can comment below or send me a message.

Ruby blocks?

Before understanding Procs and Lambda we have to understand what is a code block in Ruby:

numbers = [1, 2, 3, 4, 5]# Ruby full code blocknumbers.each do |num|   puts numend# or# Ruby one line code blocknumbers.each { |num| puts num }

The num is the argument that will be supply by the .each method. Noted that the argument name are define between two pipe characters |num|

Inside the block we can do anything including using the num argument.

Define a block using Lambda

A lambda is a way to define a block and its parameters with some special syntax. You can save this lambda into a variable for later use

display_welcome = lambda { puts 'Hello World' }# ordisplay_welcome = -> { puts 'Hello World' }#call the lambdadisplay_welcome.call

Lambda can also take arguments

display_welcome = ->(msg) { puts "Hello #{msg}" }#call the lambda with argumentsdisplay_welcome.call('Mike')

Ruby Procs

Procs are very similar to Lamdbas. Actually Lambda is a type of Proc. The syntax is a bit different:

display_welcome = Proc.new { |msg| puts "Hello #{msg}" }#call the lambda with argumentsdisplay_welcome.call('Mike')

Both Procs and Lambda will get the same result. Using one or the other can be confusing. Here a list of there differences:

  • Lambdas are defined with -> {} and procs with Proc.new {}.
  • Procs 'return' keyword return from the current calling method, while lambdas return from the lambda itself.
  • Procs dont care about the correct number of arguments, while lambdas will raise an exception.

Closures

Ruby procs & lambdas also have another special attribute. When you create a Ruby proc, it captures the current execution scope with it.

This concept, which is sometimes called closure, means that a proc will carry with it values like local variables and methods from the context where it was defined.

They dont carry the actual values, but a reference to them, so if the variables change after the proc is created, the proc will always have the latest version.

def call_proc(my_proc)  msg = 'Hello All'  my_proc.callendmsg = 'Hello Mike'my_proc = Proc.new { puts msg }puts call_proc(my_proc) # Hello Mike

Why 'Hello Mike' and not 'Hello All'? The Proc closure is using the context (scope) variables where it was define.

The mgs variable inside the call_proc method is not part of the same context (scope) as the Proc.new declaration. So have no influence on the Proc.

At this point is 100% normal that you don't understand that concept of closure. I decide to write this info here because it is related to Procs and Lambda but chance are that it is too hard to gap for the moment... And that ok! I will not ask anything about it in the following exercise don't worry...

Exercise

Create a little program that:

  • Create a variable that store a Lambda block
  • The lambda bloc will have 2 arguments.
  • Those arguments will be added (ex. a + b) inside the lambda block body.
  • Execute the lambda block
  • Display to console the result

Solution

addition = ->(a, b) { a + b }puts addition.call(10, 20) # 30

Conclusion

That's it for today. The journey just started, stay tune for the next post very soon. (later today or tomorrow)

If you have any comments or questions please do so here or send me a message on twitter.

I am new on twitter so if you want to make me happy
Follow me: Follow @justericchapman


Original Link: https://dev.to/ericchapman/sharpen-your-ruby-procs-and-lambdas-3ee8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To