[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: block_given? vs defined? yield

Yukihiro Matsumoto

2/8/2006 5:02:00 PM

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.


7 Answers

Joel VanderWerf

2/8/2006 6:45:00 PM

0

Yukihiro Matsumoto wrote:
> 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.

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.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Christian Neukirchen

2/8/2006 6:48:00 PM

0

Joel VanderWerf <vjoel@path.berkeley.edu> writes:

> Yukihiro Matsumoto wrote:
>> 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.
> 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.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Logan Capaldo

2/8/2006 7:01:00 PM

0


On Feb 8, 2006, at 12:01 PM, Yukihiro Matsumoto wrote:

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

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 Veenstra

2/8/2006 8:50:00 PM

0

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

Erik Veenstra

2/8/2006 8:52:00 PM

0

> you probably mean this:

Sorry. That's nonsense. Ignore me... :)

Florian Groß

2/9/2006 6:41:00 PM

0

David Vallner

2/9/2006 10:07:00 PM

0

Dna Š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