Block_given? vs defined? yield


#1

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


#2

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.

#3

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.


#4

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. :wink: It looks pretty
magic to me, I’d personally avoid it.


#5

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.


#6

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.)


#7

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/


#8

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.


#9

you probably mean this:

Sorry. That’s nonsense. Ignore me… :slight_smile:


#10

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.