Home>

This is a ruby ​​yield question.

This is a question by Junichi Ito, "Introduction to Ruby for those who want to become a book professional."
(Is it okay?)

def greeting
 puts'good morning'
 text = yield 'Hello'
 puts text
 puts'good evening'
end
greeting do | text |
 text * 2
end
Good morning
Hello
Hello

At yield, we understand that i am passing to block the 'Hello',
As a processing flow

  1. yield 'Hello' is passed to the block (do hereinafter) are repeated text is twice
  2. It is assigned to the text variable in def greeting ~~ end.
  3. And the text variable of 2. above is output by puts

Is there any problem with the flow?

When that happens

The text in the do | text | ~ block and the text variable in the def greeting ~~ end method
It means that they are different objects, but are they correct?