When we use `yield` inside any method who actually call this `yield` method?

I am having one problem to understand the receiver of `yield` method.

def bar
   yield
end

bar { "hello" } # "hello"

Now my question is who is calling the method `yield`. It is not `self`,
which is clear from the error below . Now what I know is if we call a
method1 inside a method2, then we can avoid to use explicit `self`, if
both are method are the instance method of the class `self.class`. But
`yield` is a method of `Proc` object. Now how `Proc` replace there the
role of `self` ? Is this the exception only with `yield` ?

def bar
   method(:yield) # undefined method 'yield' for class 'Object'
(NameError)
end

bar { "hello" } # "hello"

···

--
Posted via http://www.ruby-forum.com/.

yield calls the default block, which is a magic invisible optional proc parameter to every method call. See Blocks - Code Like This for more detail.

Note that a proc is not a method. It's a "block" or " lambda" or "functor" or "callback" or...

- Alex

···

On Feb 19, 2014, at 7:18 AM, Arup Rakshit <lists@ruby-forum.com> wrote:

I am having one problem to understand the receiver of `yield` method.

"yield" is not a method, it is a keyword of the language that triggers
hard-coded behavior in the interpreter.

Same as "def" or "while" so to speak.

Alex Chaffee wrote in post #1137217:

yield calls the default block, which is a magic invisible optional proc
parameter to every method call. See
Blocks - Code Like This for
more detail.

Nice blog it is. I watched your block/Proc lecture on YouTube, from
there I have started my journey. It was awesome.

Thanks for sharing the knowledge :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.

Xavier Noria wrote in post #1137200:

"yield" is not a method, it is a keyword of the language that triggers
hard-coded behavior in the interpreter.

Same as "def" or "while" so to speak.

Thanks for your reply. Is that `yield` is different from the one
Class: Proc (Ruby 2.0.0) ?

···

--
Posted via http://www.ruby-forum.com/\.

I think that is merely an alias for Proc#call. It will just invoke
the block. The keyword yield invokes the block which is an anonymous
function. You can do that explicitly by using #call or #yield
methods:

irb(main):006:0> def f(&b) b.call(123) end
=> nil
irb(main):007:0> f {|x| puts x}
123
=> nil

As you can see the &b stores the block as a Proc in local variable "b":

irb(main):008:0> def f(&b) p b; b.call(123) end
=> nil
irb(main):009:0> f {|x| puts x}
#<Proc:0x00000600407170@(irb):9>
123
=> nil

Kind regards

robert

···

On Wed, Feb 19, 2014 at 1:33 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Xavier Noria wrote in post #1137200:

"yield" is not a method, it is a keyword of the language that triggers
hard-coded behavior in the interpreter.

Same as "def" or "while" so to speak.

Thanks for your reply. Is that `yield` is different from the one
http://ruby-doc.org/core-2.0/Proc.html#method-i-yield ?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Xavier Noria wrote in post #1137200:

> "yield" is not a method, it is a keyword of the language that triggers
> hard-coded behavior in the interpreter.
>
> Same as "def" or "while" so to speak.

Thanks for your reply. Is that `yield` is different from the one
http://ruby-doc.org/core-2.0/Proc.html#method-i-yield ?

Proc#yield is an alias for Proc#call, that is a method of the Proc class.

But that's not what a bare yield stands for. A bare yield is a keyword
totally unrelated to Proc#yield.

This is similar to the class keyword. Object#class is a method but in order
to invoke it you need an explicit receiver, otherwise it is parsed as a
keyword.

···

On Wed, Feb 19, 2014 at 1:33 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Xavier Noria wrote in post #1137212:

···

On Wed, Feb 19, 2014 at 1:33 PM, Arup Rakshit <lists@ruby-forum.com>

Proc#yield is an alias for Proc#call, that is a method of the Proc
class.

But that's not what a bare yield stands for. A bare yield is a keyword
totally unrelated to Proc#yield.

This is similar to the class keyword. Object#class is a method but in
order
to invoke it you need an explicit receiver, otherwise it is parsed as a
keyword.

Nice explanation. :slight_smile:

Thanks to every one.

--
Posted via http://www.ruby-forum.com/\.