From: Paul Brannan Date: 2005-05-19T23:14:05+09:00 Subject: Re: syntax sugar: treating an object like a method On Thu, May 19, 2005 at 02:00:04AM +0900, Eric Mahurin wrote: > I'm thinking of yet another RCR and would like to see if > anybody likes it. I think this would be a sweet feature. Here > are the basic components: These really sound like two separate change requests to me. > 1. Allow a null/default method to be defined in a class so that > the syntax "obj(*args,&block)" could be used. obj could be any > expression yielding a object. When you try to treat that > object as a method passing arguments and/or attaching a code > block to it, the null/default method would be called. I was > thinking "@" would be a good name for this method - like "+@" > and "-@". Here are a few example uses: > > * klass(*args) : you alias the null method to "new" so that > this would be equivalent ot klass.new(*args). I tend to forget > the ".new" more often than I'd like to admit. > > * obj() : this could be aliased to clone in Object so that > default would be to just obj.clone > > * enum {...} : you could alias the null method to "each" so > that this would be equivalent to enum.each {...} What you propose requires a change in name lookup rules. I suggest you consider what the current rules are and what you would like them to be before you submit an RCR. Also, consider that Ruby already has a mechanism for callable objects; any object that is callable responds to the call() method (consider Proc and Method). Paul