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.
Daniel B. (Guest)
on 2006-02-08 18: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
Yukihiro M. (Guest)
on 2006-02-08 19:02
(Received via mailing list)
Hi,

In message "Re: block_given? vs defined? yield"
    on Thu, 9 Feb 2006 01:42:30 +0900, Daniel B.
<removed_email_address@domain.invalid> 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.
Joel VanderWerf (Guest)
on 2006-02-08 20:30
(Received via mailing list)
Yukihiro M. 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.
Joel VanderWerf (Guest)
on 2006-02-08 20:44
(Received via mailing list)
Yukihiro M. 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.
Christian N. (Guest)
on 2006-02-08 20:47
(Received via mailing list)
Joel VanderWerf <removed_email_address@domain.invalid> 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.
Logan C. (Guest)
on 2006-02-08 21:01
(Received via mailing list)
On Feb 8, 2006, at 12:01 PM, Yukihiro M. 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.)
Erik V. (Guest)
on 2006-02-08 22: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/
Erik V. (Guest)
on 2006-02-08 22:54
(Received via mailing list)
> you probably mean this:

Sorry. That's nonsense. Ignore me... :)
Florian GroÃ? (Guest)
on 2006-02-09 20:41
(Received via mailing list)
Logan C. 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.
David V. (Guest)
on 2006-02-10 00:06
(Received via mailing list)
DÅ?a Å tvrtok 09 Február 2006 19:40 Florian GroÃ? napísal:
> Logan C. 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 V.
This topic is locked and can not be replied to.