PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Re: def blah do |x| -- alternate method definition syntax
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: def blah do |x| -- alternate method definition syntax

Réponse
 
LinkBack Outils de la discussion
Vieux 05/11/2007, 05h24   #1
matz@ruby-lang.org
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: def blah do |x| -- alternate method definition syntax

Hi,

In message "Re: def blah do |x| -- alternate method definition syntax"
on Mon, 5 Nov 2007 14:07:21 +0900, Joe Holt <joe07734@gmail.com> writes:

|Friend wondered why a method definition couldn't be like this:
|
|def blah do |x|
| ...
|end

This syntax disallows optional arguments. We can't just parse them.
If you really want to define a method with block parameter style, you
can do:

define_method(:blah) do |x|
...
end

Almost same, isn't it?

matz.

  Réponse avec citation
Vieux 05/11/2007, 11h59   #2
joe07734@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: def blah do |x| -- alternate method definition syntax

Thanks matz and all. The define_method() trick hadn't occurred to me
-- almost the same, indeed. Why couldn't block parameter syntax be
extended to accept optional args: do |x, y=0| ? I Realize that this is
only meaningful for the hypothetical def usage, but come to think of
it I see no reason blocks couldn't accept optional args in cases where
they're .called().

And doesn't calling a proc with [] smell like a cute hack (hey, we've
got this bracket method...) to anyone else?

On Nov 5, 2007, at 12:24 AM, Yukihiro Matsumoto wrote:

> Hi,
>
> In message "Re: def blah do |x| -- alternate method definition syntax"
> on Mon, 5 Nov 2007 14:07:21 +0900, Joe Holt <joe07734@gmail.com>
> writes:
>
> |Friend wondered why a method definition couldn't be like this:
> |
> |def blah do |x|
> | ...
> |end
>
> This syntax disallows optional arguments. We can't just parse them.
> If you really want to define a method with block parameter style, you
> can do:
>
> define_method(:blah) do |x|
> ...
> end
>
> Almost same, isn't it?
>
> matz.
>



  Réponse avec citation
Vieux 05/11/2007, 12h40   #3
halostatue@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: def blah do |x| -- alternate method definition syntax

On 11/5/07, Joe Holt <joe07734@gmail.com> wrote:
> Thanks matz and all. The define_method() trick hadn't occurred to me
> -- almost the same, indeed. Why couldn't block parameter syntax be
> extended to accept optional args: do |x, y=0| ? I Realize that this is
> only meaningful for the hypothetical def usage, but come to think of
> it I see no reason blocks couldn't accept optional args in cases where
> they're .called().
>
> And doesn't calling a proc with [] smell like a cute hack (hey, we've
> got this bracket method...) to anyone else?


Nope. Not something that seems odd at all after five years of using Ruby.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
* austin@halostatue.ca * http://www.halostatue.ca/feed/
* austin@zieglers.ca

  Réponse avec citation
Vieux 05/11/2007, 13h41   #4
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: def blah do |x| -- alternate method definition syntax

On Nov 5, 4:59 am, joe07...@gmail.com wrote:
> Thanks matz and all. The define_method() trick hadn't occurred to me
> -- almost the same, indeed. Why couldn't block parameter syntax be
> extended to accept optional args: do |x, y=0| ? I Realize that this is
> only meaningful for the hypothetical def usage, but come to think of
> it I see no reason blocks couldn't accept optional args in cases where
> they're .called().


Because the | operator exists, and there are some edge cases where it
would be hard to determine when the block had ended. Something like:

foo{ |x, y=0| 1+6|7 }

could be either:
foo{ |x, y=0| (1+6|7) }
or
foo{ |x, y=((0|1)+6)| 7 }


IIRC, it's not just that such edge cases exist (where a sane human
could possibly make a choice to disallow certain otherwise-legal
syntax), but that it's very hard or impossible to adjust the syntax
lexer to allow them.

  Réponse avec citation
Vieux 05/11/2007, 13h50   #5
robert.dober@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: def blah do |x| -- alternate method definition syntax

On 11/5/07, Joe Holt <joe07734@gmail.com> wrote:
> Thanks matz and all. The define_method() trick hadn't occurred to me
> -- almost the same, indeed. Why couldn't block parameter syntax be
> extended to accept optional args: do |x, y=0| ? I Realize that this is
> only meaningful for the hypothetical def usage,

not at all, it is very useful and will therefore be part of Ruby1.9
> but come to think of
> it I see no reason blocks couldn't accept optional args in cases where
> they're .called().

Neither did Matz
>

<snip>

Robert

--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

  Réponse avec citation
Vieux 05/11/2007, 13h52   #6
robert.dober@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: def blah do |x| -- alternate method definition syntax

On 11/5/07, Phrogz <phrogz@mac.com> wrote:
> On Nov 5, 4:59 am, joe07...@gmail.com wrote:
> > Thanks matz and all. The define_method() trick hadn't occurred to me
> > -- almost the same, indeed. Why couldn't block parameter syntax be
> > extended to accept optional args: do |x, y=0| ? I Realize that this is
> > only meaningful for the hypothetical def usage, but come to think of
> > it I see no reason blocks couldn't accept optional args in cases where
> > they're .called().

>
> Because the | operator exists, and there are some edge cases where it
> would be hard to determine when the block had ended. Something like:
>
> foo{ |x, y=0| 1+6|7 }
>
> could be either:
> foo{ |x, y=0| (1+6|7) }
> or
> foo{ |x, y=((0|1)+6)| 7 }
>
>
> IIRC, it's not just that such edge cases exist (where a sane human
> could possibly make a choice to disallow certain otherwise-legal
> syntax), but that it's very hard or impossible to adjust the syntax
> lexer to allow them.

ooops, sorry I was replying to the wrong question

foo { |x,*args,&blk| } will be part of Ruby1.9 and not
foo{ |x,y=42|}
OP can you please post questions that correspond to my answers? Ty very much
R.

--
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14874 seconds with 14 queries