Ruby Concept - Exit

Now I am working on Ruby concepts. Based on the background of VB, there are some concepts that I do not quite understand yet. Income is one of them. I understand how this works in a practical sense, but does not see the value of Yield, or when and how I will use it in full.

+4
source share
3 answers

Profitability is part of the larger closure system in Ruby. This is a very important part of the language, and you will find it in every Ruby script you come across.

http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/

+4
source

It’s good to understand how income works, but I rarely use it and thought the same is true for others. Comments on this answer may indicate otherwise.

The yield Ruby statement passes the control to the block given to the method. After the block is completed, the control returns to the method and continues executing the instruction immediately after exiting.

Here's a variant of the excessive Fibonacci sequence

def fib(upto) curr, succ = 1, 1 while curr <= upto puts "before" yield curr puts "after" curr, succ = succ, curr+succ end end 

Then you call the method with something like

 fib(8) {|res| puts res} 

and the output will be

 before 1 after before 1 after before 2 after before 3 after before 5 after before 8 after 
+1
source

All Articles