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

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-19 13:18
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"
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (fxn)
on 2014-02-19 13:25
(Received via mailing list)
"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.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-19 13:33
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 ?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-02-19 15:12
(Received via mailing list)
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 ?

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
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (fxn)
on 2014-02-19 15:15
(Received via mailing list)
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 ?


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.
5a0d7f0cb2fac7858d234de7f7f01491?d=identicon&s=25 Alex Chaffee (Guest)
on 2014-02-19 15:55
(Received via mailing list)
yield calls the default block, which is a magic invisible optional proc
parameter to every method call. See
http://codelikethis.com/lessons/ruby_blocks/blocks... for
more detail.

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

 - Alex
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-19 16:38
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. :-)

Thanks to every one.
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-19 21:28
Alex Chaffee wrote in post #1137217:
> yield calls the default block, which is a magic invisible optional proc
> parameter to every method call. See
> http://codelikethis.com/lessons/ruby_blocks/blocks... 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 :-)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.