[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: RCR 296: Destructive methods return self

Berger, Daniel

3/22/2005 5:18:00 PM

> -----Original Message-----
> From: Malte Milatz [mailto:malteNOSPAM@gmx-topmail.de]
> Sent: Tuesday, March 22, 2005 10:00 AM
> To: ruby-talk ML
> Subject: Re: RCR 296: Destructive methods return self
>
>
> Christian Neukirchen:
> >>> gsub! returns self
> >>> gsub!? returns self or nil (yes, it looks silly)
> > I actually like that. :-)
>
> +1
>
> Malte

Hm...actually this reminds me of a Perl module called "Want" by Robin
Houston. With that module, you could get extremely fine control over
calling context. For example, you could do a sort of "lookahead" to, in
effect, see if the method was chained or not:

use Want;

sub foo{
...
if(want("OBJECT")){
return $self;
}
else{
# return some other value based on context
}
}

I used this module in my Set::Array, Set::Hash and Set::String Perl
modules and was quite happy with it.

Could something like "object_wanted?" be added? Then you, the code
writer, could solve the "self or nil" issue yourself like this:

class Foo
def some_method!
if object_wanted?
return self
end
# otherwise, return nil (or whatever)
end
end

You can take a look at Want at
http://cpansearch.perl.org/~robin/Want-0....

Just a thought. Now I suppose someone is going to tell me you could do
this all along. ;)

Regards,

Dan