Forum: Ruby block_given? vs defined? yield

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Daniel Berger (Guest)
on 2006-02-08 17:43
(Received via mailing list)
Hi all,

Is there any difference between "block_given?" vs "defined? yield" ?

I dug through the source a bit, but I had trouble finding the
definition.  Is
the former just a wrapper for the latter?

Thanks,

Dan

PS - Found the latter in open3.rb
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 Yukihiro Matsumoto (Guest)
on 2006-02-08 18:02
(Received via mailing list)
Hi,

In message "Re: block_given? vs defined? yield"
    on Thu, 9 Feb 2006 01:42:30 +0900, Daniel Berger
<Daniel.Berger@qwest.com> writes:

|Is there any difference between "block_given?" vs "defined? yield" ?

They are almost same.  The only difference is the former is a method,
and the latter is a syntax (no call), and consequently the latter
might be a little bit faster, but practically you can consider them
same.

							matz.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-02-08 19:30
(Received via mailing list)
Yukihiro Matsumoto wrote:
> same.
>
> 							matz.

I didn't know block_given? was a method. So there's another possible
difference besides speed:

irb(main):001:0> class X
irb(main):002:1>   def block_given?; false; end
irb(main):003:1>   def foo; yield if block_given?; end
irb(main):004:1>   end
=> nil
irb(main):005:0> X.new.foo {puts "FOO"}
=> nil

Just a bizarre thought.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2006-02-08 19:44
(Received via mailing list)
Yukihiro Matsumoto wrote:
> same.
>
> 							matz.

I didn't know block_given? was a method. So there's another possible
difference besides speed:

irb(main):001:0> class X
irb(main):002:1>   def block_given?; false; end
irb(main):003:1>   def foo; yield if block_given?; end
irb(main):004:1>   end
=> nil
irb(main):005:0> X.new.foo {puts "FOO"}
=> nil

Just a bizarre thought.
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-02-08 19:47
(Received via mailing list)
Joel VanderWerf <vjoel@path.berkeley.edu> writes:

>> might be a little bit faster, but practically you can consider them
>> same.
>>
>> 							matz.
> I didn't know block_given? was a method. So there's another possible
> difference besides speed:

And I didn't know "defined? yield" even existed. ;-) It looks pretty
magic to me, I'd personally avoid it.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-02-08 20:01
(Received via mailing list)
On Feb 8, 2006, at 12:01 PM, Yukihiro Matsumoto wrote:

> might be a little bit faster, but practically you can consider them
> same.
>
> 							matz.
>

I didn't know yield could be considered "defined" or not. Does this
mean yield is not a key word?

irb(main):014:0> def quby
irb(main):015:1>   puts hello
irb(main):016:1>
irb(main):017:1> end
=> nil
irb(main):018:0> defined? quby
=> "method"

Gasp!

irb(main):020:0> def with_ablock
irb(main):021:1>   p defined? yield
irb(main):022:1> end
=> nil
irb(main):023:0> with_ablock { 1 }
"yield"
=> nil
irb(main):024:0> with_ablock
nil
=> nil

Well that shows home much I know about the operation of defined?
(that is to say very little.)

So is yield a method that only has scope inside the calling method? I
tired one of these method(:yield) but it didn't work. (Nor should it,
even if my previous statement is correct). I thought maybe defined?
worked for keywords, but that doesn't really make sense. Or is
defined? yield just a "hack" (I use "hack" in the nicest way
possible.) and yield is a keyword, its just that block_given? has to
have some way to be implemented and you didn't want to make
block_given? a keyword (which makes sense, the fewer the keywords the
better, IMO.)
4b38034fdb66886a91d55fcd12964cee?d=identicon&s=25 Erik Veenstra (Guest)
on 2006-02-08 21:53
(Received via mailing list)
> p defined? yield

you probably mean this:

 p defined?(:yield)

:yield - a symbol, indicating the method yield
yield  - a call to the block

gegroet,
Erik V. - http://www.erikveen.dds.nl/
4b38034fdb66886a91d55fcd12964cee?d=identicon&s=25 Erik Veenstra (Guest)
on 2006-02-08 21:54
(Received via mailing list)
> you probably mean this:

Sorry. That's nonsense. Ignore me... :)
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian GroÃ? (Guest)
on 2006-02-09 19:41
(Received via mailing list)
Logan Capaldo wrote:

> I didn't know yield could be considered "defined" or not. Does this
> mean yield is not a key word?

defined? will usually change the meaning of things following it.
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-02-09 23:06
(Received via mailing list)
DÅ?a Å tvrtok 09 Február 2006 19:40 Florian GroÃ? napísal:
> Logan Capaldo wrote:
> > I didn't know yield could be considered "defined" or not. Does this
> > mean yield is not a key word?
>
> defined? will usually change the meaning of things following it.

On a slightly related note, "defined? def" doesn't seem to parse as a
complete
expression, and the "defined? return" and "defined? break" return nil.
Seems
yield is considered special.

Eeek. Special cases.

David Vallner
This topic is locked and can not be replied to.