[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Passive Arguments

T. Onoma

1/16/2005 3:32:00 PM

Since Ruby doesn't support signature-based method definitions, what good does
it do to throw errors on the wrong number of arguments? Seems to me, this
just makes the programmers job more difficult. What if Ruby nil'd unspecified
parameters and ignored extra parameters. Eg.

def ameth(a,b)
a.to_i + b.to_i
end

ameth #=> 0
ameth(1) #=> 1
ameth(1,2,3) #=> 3

Perhaps there is a problem with doing this that I don't see, but it's more in
line with Ruby's dynamic behavior. Testing is were such errors should be
caught, if indeed these are errors. This is the standard explanation one gets
about duck-typing after all. Why wouldn't it apply here as well? And it's not
like it can't be done either. As it stands, to achieve the same effect I must
conceive of it a bit differently as:

def ameth( *args )
args[0].to_i + args[1].to_i
end

So it's not like it should not be done. We do it all the time.

Okay so this leads me to my next thought about passive arguments. As you all
probably know I've been working on AOP for awhile and one thing to come up is
how to deal with the method signatures of advice. Since one advice method can
wrap multiple regular methods there's the problem of dealing with variant
signatures. For example look at this psuedo-code:

def x(a)
...

def y(a,b)
...

def w
puts "Advising"
super # calls the advised method
end

wrap :w => [ :x, :y ]

So #w wraps both #x and and #y. Obviously there's going to be a problem with
the arguments. The fix is of course to do 'def w(*args, &blk)' instead. And
indeed as far as wraps go one will ALWAYS end up having to do that. It would
be much nicer if one could do 'def w(a,b)' and it would still work --hence
the beginning of this post.

Also, since there will be occasions when the args won't matter it might be
nice too, if not specifying any parameters meant that they were simply passed
on through to super. In other words:

def w
puts "Advising"
super # calls the advised method
end

were exactly the same as doing:

def w(*args, &blk)
puts "Advising"
super(*args, &blk) # calls the advised method
end

and to actually change the interface one would have to do so explicitly, just
as one has to be with #super currently:

def w() # changes the interface
puts "Advising"
super # calls the advised method
end

What do you think?
T.


11 Answers

Esteban Manchado Velázquez

1/16/2005 9:50:00 PM

0

Hi,

On Mon, Jan 17, 2005 at 12:32:08AM +0900, trans. (T. Onoma) wrote:
> Since Ruby doesn't support signature-based method definitions, what good does
> it do to throw errors on the wrong number of arguments? Seems to me, this
> just makes the programmers job more difficult. What if Ruby nil'd unspecified
> parameters and ignored extra parameters. Eg.
>
> def ameth(a,b)
> a.to_i + b.to_i
> end
>
> ameth #=> 0
> ameth(1) #=> 1
> ameth(1,2,3) #=> 3

¡Arggh! Please don't. This is the Perl way, and I've had some good
headaches because of this, while refactoring code.

> Perhaps there is a problem with doing this that I don't see, but it's more in
> line with Ruby's dynamic behavior. Testing is were such errors should be
> caught, if indeed these are errors. This is the standard explanation one gets
> about duck-typing after all. Why wouldn't it apply here as well? And it's not
> like it can't be done either. As it stands, to achieve the same effect I must
> conceive of it a bit differently as:
>
> def ameth( *args )
> args[0].to_i + args[1].to_i
> end
>
> So it's not like it should not be done. We do it all the time.

But, IMHO, that's a pretty _rare_ need. If you need that, you can use
"*args" (as you say) or perhaps use optional parameters. But once you don't
raise an error when calling a method with an unexpected number of arguments,
you have _no automatic check_ for _most_ (again, IMHO) cases.

Regards,

--
Esteban Manchado Velázquez <zoso@foton.es> - http://ww...
EuropeSwPatentFree - http://EuropeSwPatentFree.his...


T. Onoma

1/17/2005 11:34:00 AM

0

On Sunday 16 January 2005 04:50 pm, Esteban Manchado Velázquez wrote:
| Hi,
|
| On Mon, Jan 17, 2005 at 12:32:08AM +0900, trans. (T. Onoma) wrote:
| > Since Ruby doesn't support signature-based method definitions, what good
| > does it do to throw errors on the wrong number of arguments? Seems to me,
| > this just makes the programmers job more difficult. What if Ruby nil'd
| > unspecified parameters and ignored extra parameters. Eg.
| >
| > def ameth(a,b)
| > a.to_i + b.to_i
| > end
| >
| > ameth #=> 0
| > ameth(1) #=> 1
| > ameth(1,2,3) #=> 3
|
| ¡Arggh! Please don't. This is the Perl way, and I've had some good
| headaches because of this, while refactoring code.


Ouch, A Spanish expletive! :) Could you elaborate on the problem arising from
refactoring? Thanks.






benny

1/17/2005 11:42:00 AM

0

trans. (T. Onoma) wrote:

> In other words:
>
> def w
> puts "Advising"
> super  # calls the advised method
> end
>
> were exactly the same as doing:
>
> def w(*args, &blk)
> puts "Advising"
> super(*args, &blk)  # calls the advised method
> end
>

I think this would be too much magic behind the scenes.
At the moment super ist behaving like just another method, i.e.
you can imagine how it behaves.

what if super may have arguments but you don't want to give any to it?
making super != super() is not an option.

in the most cases where I passed the same arguments to super that are given
to the method itself I figured out (after a while :) ) that I could have
made a better class design. IMHO it is more logical and usual to have no
equality in arguments.

benny

T. Onoma

1/17/2005 12:18:00 PM

0

On Monday 17 January 2005 06:46 am, benny wrote:
| trans. (T. Onoma) wrote:
| > In other words:
| >
| > def w
| > puts "Advising"
| > super  # calls the advised method
| > end
| >
| > were exactly the same as doing:
| >
| > def w(*args, &blk)
| > puts "Advising"
| > super(*args, &blk)  # calls the advised method
| > end
|
| I think this would be too much magic behind the scenes.
| At the moment super ist behaving like just another method, i.e.
| you can imagine how it behaves.

Yes, well almost -- 'super' without args will pass on method args.

| what if super may have arguments but you don't want to give any to it?
| making super != super() is not an option.

Hmm...maybe I was not clear enough on this point (or I misunderstand) using
'super()' would NOT pass args to parent method, but just 'super' does pass
arguments --just as it is now. The change only comes in the 'def w', where no
arguments passes them through unchanged too.

| in the most cases where I passed the same arguments to super that are given
| to the method itself I figured out (after a while :) ) that I could have
| made a better class design. IMHO it is more logical and usual to have no
| equality in arguments.

Interesting thought, but it seems Matz thought it was common enough to have
special behavior on super, so I'm not sure.




benny

1/17/2005 12:31:00 PM

0

trans. (T. Onoma) wrote:


> | making super != super() is not an option.
>
> Hmm...maybe I was not clear enough on this point (or I misunderstand)
> using 'super()' would NOT pass args to parent method, but just 'super'
> does pass arguments --just as it is now.
Oh! I was sure that they are not passed by default, but I checked it and you
are right. it must be that I always use the explicit argument transfer or
did that behaviour change somewhere between 1.6 and 1.8.2?

but I really find it suprising that super != super() in contrast to every
other method.

learned a lesson today, thanks!

regards,

benny

never

6/8/2008 4:46:00 PM

0

On Sun, 08 Jun 2008 08:29:39 -0700, Rudy Canoza
<pipes@thedismalscience.noot> wrote:

>strategy26 wrote:
>> "Rudy Canoza" <pipes@thedismalscience.noot> wrote in message news:coCdnfui1JUEwdbVnZ2dnUVZ_uSdnZ2d@earthlink.com...
>>> "We 'need' gasoline," usually shrieked in a whiny, crybaby high pitch.
>>>
>>> No, we don't "need" gasoline at all. No one "needs" gasoline.
>>>
>>> You only need things to do things you *want* to do. You WANT to drive,
>>> and in order to do so you need gasoline (or diesel). You WANT to play
>>> golf, and in order to do so you need golf clubs and golf balls. You
>>> WANT to go on living, and in order to do so you need food, water,
>>> oxygen, shelter, etc.
>>>
>>> Any one person or family can live a lifestyle such that they don't
>>> consume a drop of gasoline themselves. If any one family can do it,
>>> then lots of families can do it. Here's how you do it: you live in a
>>> city, close to shops and schools, and you take public transportation or
>>> you ride bicycles or you walk. You buy zero gasoline. The End.
>>>
>>> Cue high pitched whiny shrieking voice: "But I don't *want* to live in
>>> the city. I want to live on a sterile, soul-destroying cul-de-sac far
>>> from the urban core." Fine. Then, in order to get around, you're going
>>> to feel you "need" a car, and you're going to have to buy a lot of
>>> high-priced (and certain to rise in price) gasoline. But don't tell us
>>> you "need" gasoline, because you don't; you *WANT* gasoline, in order to
>>> live the lifestyle that you don't "need" to live in the first place.
>>> You *could* live in the city and buy no gasoline, but you choose to live
>>> a lifestyle that requires gasoline. Your choice. Pay the price for
>>> your choice, and shut your fucking whiny shrieking mouth.
>>
>> I wanna argue that we need windmills and electric cars.
>
>We don't "need" anything. We want things, and stuff you think you
>"need" is only to satisfy those wants.


Srt of like a breath of clean air.

DCI

Major Debacle

6/8/2008 4:57:00 PM

0

Rudy Canoza wrote:
> strategy26 wrote:
>> "Rudy Canoza" <pipes@thedismalscience.noot> wrote in message news:coCdnfui1JUEwdbVnZ2dnUVZ_uSdnZ2d@earthlink.com...
>>> "We 'need' gasoline," usually shrieked in a whiny, crybaby high pitch.
>>>
>>> No, we don't "need" gasoline at all. No one "needs" gasoline.
>>>
>>> You only need things to do things you *want* to do. You WANT to drive,
>>> and in order to do so you need gasoline (or diesel). You WANT to play
>>> golf, and in order to do so you need golf clubs and golf balls. You
>>> WANT to go on living, and in order to do so you need food, water,
>>> oxygen, shelter, etc.
>>>
>>> Any one person or family can live a lifestyle such that they don't
>>> consume a drop of gasoline themselves. If any one family can do it,
>>> then lots of families can do it. Here's how you do it: you live in a
>>> city, close to shops and schools, and you take public transportation or
>>> you ride bicycles or you walk. You buy zero gasoline. The End.
>>>
>>> Cue high pitched whiny shrieking voice: "But I don't *want* to live in
>>> the city. I want to live on a sterile, soul-destroying cul-de-sac far
>>> from the urban core." Fine. Then, in order to get around, you're going
>>> to feel you "need" a car, and you're going to have to buy a lot of
>>> high-priced (and certain to rise in price) gasoline. But don't tell us
>>> you "need" gasoline, because you don't; you *WANT* gasoline, in order to
>>> live the lifestyle that you don't "need" to live in the first place.
>>> You *could* live in the city and buy no gasoline, but you choose to live
>>> a lifestyle that requires gasoline. Your choice. Pay the price for
>>> your choice, and shut your fucking whiny shrieking mouth.
>> I wanna argue that we need windmills and electric cars.
>
> We don't "need" anything. We want things, and stuff you think you
> "need" is only to satisfy those wants.

Sounds like your solution to the energy crisis is for everyone to stop
wanting to live.

I don't need food and water. I only *want* them.

We may be able to avoid buying gasoline/diesel directly. But it is much
more difficult to avoid creating an indirect demand for gasoline/diesel
by not consuming anything that required the use of gasoline/diesel to
create and distribute... especially if you live in the city.

--
Good Earth Original Herbal Tea contains *Artificial Flavoring*

kT

6/8/2008 5:06:00 PM

0

Major Debacle wrote:
> Rudy Canoza wrote:
>> strategy26 wrote:
>>> "Rudy Canoza" <pipes@thedismalscience.noot> wrote in message
>>> news:coCdnfui1JUEwdbVnZ2dnUVZ_uSdnZ2d@earthlink.com...
>>>> "We 'need' gasoline," usually shrieked in a whiny, crybaby high pitch.
>>>>
>>>> No, we don't "need" gasoline at all. No one "needs" gasoline.
>>>>
>>>> You only need things to do things you *want* to do. You WANT to
>>>> drive, and in order to do so you need gasoline (or diesel). You
>>>> WANT to play golf, and in order to do so you need golf clubs and
>>>> golf balls. You WANT to go on living, and in order to do so you
>>>> need food, water, oxygen, shelter, etc.
>>>>
>>>> Any one person or family can live a lifestyle such that they don't
>>>> consume a drop of gasoline themselves. If any one family can do it,
>>>> then lots of families can do it. Here's how you do it: you live in
>>>> a city, close to shops and schools, and you take public
>>>> transportation or you ride bicycles or you walk. You buy zero
>>>> gasoline. The End.
>>>>
>>>> Cue high pitched whiny shrieking voice: "But I don't *want* to live
>>>> in the city. I want to live on a sterile, soul-destroying
>>>> cul-de-sac far from the urban core." Fine. Then, in order to get
>>>> around, you're going to feel you "need" a car, and you're going to
>>>> have to buy a lot of high-priced (and certain to rise in price)
>>>> gasoline. But don't tell us you "need" gasoline, because you don't;
>>>> you *WANT* gasoline, in order to live the lifestyle that you don't
>>>> "need" to live in the first place. You *could* live in the city and
>>>> buy no gasoline, but you choose to live a lifestyle that requires
>>>> gasoline. Your choice. Pay the price for your choice, and shut
>>>> your fucking whiny shrieking mouth.
>>> I wanna argue that we need windmills and electric cars.
>>
>> We don't "need" anything. We want things, and stuff you think you
>> "need" is only to satisfy those wants.
>
> Sounds like your solution to the energy crisis is for everyone to stop
> wanting to live.
>
> I don't need food and water. I only *want* them.
>
> We may be able to avoid buying gasoline/diesel directly. But it is much
> more difficult to avoid creating an indirect demand for gasoline/diesel
> by not consuming anything that required the use of gasoline/diesel to
> create and distribute... especially if you live in the city.

Well then, you don't *NEED* to kill babies to get the oil and gas that
you don't *NEED*, you only *WANT* to kill babies for oil and gas.

I rest my case.
>

never

6/8/2008 5:19:00 PM

0

On Sun, 08 Jun 2008 09:57:03 -0700, Major Debacle
<Major_Debacle@the_Pentagon.mil> wrote:

>Rudy Canoza wrote:
>> strategy26 wrote:
>>> "Rudy Canoza" <pipes@thedismalscience.noot> wrote in message news:coCdnfui1JUEwdbVnZ2dnUVZ_uSdnZ2d@earthlink.com...
>>>> "We 'need' gasoline," usually shrieked in a whiny, crybaby high pitch.
>>>>
>>>> No, we don't "need" gasoline at all. No one "needs" gasoline.
>>>>
>>>> You only need things to do things you *want* to do. You WANT to drive,
>>>> and in order to do so you need gasoline (or diesel). You WANT to play
>>>> golf, and in order to do so you need golf clubs and golf balls. You
>>>> WANT to go on living, and in order to do so you need food, water,
>>>> oxygen, shelter, etc.
>>>>
>>>> Any one person or family can live a lifestyle such that they don't
>>>> consume a drop of gasoline themselves. If any one family can do it,
>>>> then lots of families can do it. Here's how you do it: you live in a
>>>> city, close to shops and schools, and you take public transportation or
>>>> you ride bicycles or you walk. You buy zero gasoline. The End.
>>>>
>>>> Cue high pitched whiny shrieking voice: "But I don't *want* to live in
>>>> the city. I want to live on a sterile, soul-destroying cul-de-sac far
>>>> from the urban core." Fine. Then, in order to get around, you're going
>>>> to feel you "need" a car, and you're going to have to buy a lot of
>>>> high-priced (and certain to rise in price) gasoline. But don't tell us
>>>> you "need" gasoline, because you don't; you *WANT* gasoline, in order to
>>>> live the lifestyle that you don't "need" to live in the first place.
>>>> You *could* live in the city and buy no gasoline, but you choose to live
>>>> a lifestyle that requires gasoline. Your choice. Pay the price for
>>>> your choice, and shut your fucking whiny shrieking mouth.
>>> I wanna argue that we need windmills and electric cars.
>>
>> We don't "need" anything. We want things, and stuff you think you
>> "need" is only to satisfy those wants.
>
>Sounds like your solution to the energy crisis is for everyone to stop
>wanting to live.
>
>I don't need food and water. I only *want* them.
>
>We may be able to avoid buying gasoline/diesel directly. But it is much
>more difficult to avoid creating an indirect demand for gasoline/diesel
>by not consuming anything that required the use of gasoline/diesel to
>create and distribute... especially if you live in the city.

I don't think Rudy is suggesting a solution to the energy crisis is to
stop living. He be suggesting that the drivers evaluate why they're
driving around a gas guzzling automobile when they could walk, use a
bicycle, or ride public transportation. Or maybe just staying home and
enjoying life. It's all about rethinking what we're doing with our
energy situation.

DCI

Rudy Canoza

6/8/2008 7:34:00 PM

0

Major Debacle wrote:
> Rudy Canoza wrote:
>> strategy26 wrote:
>>> "Rudy Canoza" <pipes@thedismalscience.noot> wrote in message
>>> news:coCdnfui1JUEwdbVnZ2dnUVZ_uSdnZ2d@earthlink.com...
>>>> "We 'need' gasoline," usually shrieked in a whiny, crybaby high pitch.
>>>>
>>>> No, we don't "need" gasoline at all. No one "needs" gasoline.
>>>>
>>>> You only need things to do things you *want* to do. You WANT to
>>>> drive, and in order to do so you need gasoline (or diesel). You
>>>> WANT to play golf, and in order to do so you need golf clubs and
>>>> golf balls. You WANT to go on living, and in order to do so you
>>>> need food, water, oxygen, shelter, etc.
>>>>
>>>> Any one person or family can live a lifestyle such that they don't
>>>> consume a drop of gasoline themselves. If any one family can do it,
>>>> then lots of families can do it. Here's how you do it: you live in
>>>> a city, close to shops and schools, and you take public
>>>> transportation or you ride bicycles or you walk. You buy zero
>>>> gasoline. The End.
>>>>
>>>> Cue high pitched whiny shrieking voice: "But I don't *want* to live
>>>> in the city. I want to live on a sterile, soul-destroying
>>>> cul-de-sac far from the urban core." Fine. Then, in order to get
>>>> around, you're going to feel you "need" a car, and you're going to
>>>> have to buy a lot of high-priced (and certain to rise in price)
>>>> gasoline. But don't tell us you "need" gasoline, because you don't;
>>>> you *WANT* gasoline, in order to live the lifestyle that you don't
>>>> "need" to live in the first place. You *could* live in the city and
>>>> buy no gasoline, but you choose to live a lifestyle that requires
>>>> gasoline. Your choice. Pay the price for your choice, and shut
>>>> your fucking whiny shrieking mouth.
>>> I wanna argue that we need windmills and electric cars.
>>
>> We don't "need" anything. We want things, and stuff you think you
>> "need" is only to satisfy those wants.
>
> Sounds like your solution to the energy crisis is for everyone to stop
> wanting to live.

No, not at all. They need to live differently.


> I don't need food and water. I only *want* them.

No, you want to live, and you need food and water in order to attain
that. Almost everyone wants to live, and in order to do that they need
food and water. But there is no *absolute* need for food and water,
such that food and water are "needs" while tickets to the opera are a
"mere want". It's this pejorative view of the things people classify as
"mere wants" that is objectionable. People want to go to the opera, and
in order to get in they need opera tickets. So opera tickets are a
need, if your wants include attending the opera.

Everything is ultimately a want.


>
> We may be able to avoid buying gasoline/diesel directly. But it is much
> more difficult to avoid creating an indirect demand for gasoline/diesel
> by not consuming anything that required the use of gasoline/diesel to
> create and distribute... especially if you live in the city.

Our society could be organize, and soon probably will be organized, such
that we will use much less gasoline than before. If *any* part of it is
not a "need", then none of it is.