From: Guoliang Cao Date: 2008-05-20T23:44:35+09:00 Subject: Re: Method as first class member Robert Klemme wrote: > On 19.05.2008 22:28, Guoliang Cao wrote: >> object" - a method is an object too. This also makes Ruby a next level >> functional language I guess. To refer to the method object, we probably >> can use syntax like this: >> >> m = obj->do_something >> m[1,2,3] >> >> m = Dir->pwd >> m[] > > This does exist already although the syntax is slightly different: > > irb(main):003:0> RUBY_VERSION > => "1.8.5" > irb(main):004:0> m = "foo".method :length > => # > irb(main):005:0> m.call > => 3 > irb(main):006:0> m = String.instance_method :length > => # > irb(main):007:0> m.bind("foo").call > => 3 > irb(main):008:0> m.bind("foo") > => # > irb(main):009:0> m.bind("foo")[] > => 3 > irb(main):010:0> m = "foo".method(:<<) > => # > irb(main):011:0> m["bar"] > => "foobar" > irb(main):012:0> > > Note also that you can achieve something similar via send and this is > often sufficient. > Thank you for replying. I'm aware of those even though I didn't use them so far. "Methods are objects" are true. I'd better rephrase my suggestion as a syntax sugar or enhancement. >> Method object may work similar to lambda and be passed as parameter. >> Down the way, partial invocation can be added by associating a context >> to the method object and memorizing parameter values. i.e. >> >> m = obj->do_something 1,2,3 >> m[] > > This is only possible using lambdas AFAIK, i.e. you cannot stick method > parameters into a method. > >> What do you guys think? any comments are welcome. > > With these questions I always ask myself what do we gain by such a > change? All changes have some effort attached, some more and some less. > But it's not worthwhile if the effort outweighs the benefits. What do > you think are the benefits of this change? Why should we do it? > I believe we get shorter, more intuitive code with this kind of syntax, and partial invocation can help DRY in a lot of cases. Compare below examples, what do you prefer? IMHO, the former syntax attracts programmers to use, the latter is like, do this if you have to(this may not be a bad thing though) "foo"->length "foo".method :length I do agree changes require efforts and also add learning burden to users, language designers/implementers have to be very careful with any change to the language. However, most programmers have a dream - a perfect language. Even it may not become true, we are closer than ever. Do you agree? -- Posted via http://www.ruby-forum.com/.