Issue #5364 has been updated by Yusuke Endoh.

Status changed from Feedback to Open

Hello,

To prevent `bikeshed' discussion, we should focus use case first
about this ticket, I think.  Why don't you talk about the concrete
notation after that?


I come up with some use cases.  First case:

  ary.map {|x| x+3 }
  ary.map(&3.\+)

I like the latter not only because it is shorter, but also because
I don't have to bother about the name of the temporal variable.
We can already write ary.map(&:succ) in 1.9, so I think this style
will become more in demand.
Indeed, there is a trade-off between readability and writability.
I think it is a matter of getting familiar with it, though.


Another one for DSL.  Consider rspec example:

  lambda { foo.bar(baz) }.should raise(SomeError)
  foo.\bar.with(baz).should raise(SomeError)

I think the latter is more readable than the formar.
I guess there are many possible use case in this field.


Finally, use case in future.
Ruby's Method class is poor currently.
I believe this proposal will encourage us to enrith Method class.
For example, I often use Object#method to glance over an unfamiliar
library:

  irb(main):001:0> unfamiliar_obj.methods - 0.methods
  [..., interesting_method, ...]

After I find an interesting method, I also want to read the rdoc
in similar way:

  irb(main):002:0> unfamiliar_obj.\interesting_method.doc

Of course there is no Method#doc currently.  I think this shorthand
notation will enrich such a feature.

-- 
Yusuke Endoh <mame / tsg.ne.jp>
----------------------------------------
Feature #5364: How about new syntax: "object.\method" returns a Method instance?
http://redmine.ruby-lang.org/issues/5364

Author: Joey Zhou
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 


I'm afraid the normal way of creating a Method instance is circuitous and a bit hard to write:

    a_method = object.method(:method_name)

I find the way of obtaining a function/method object in Python is so easy, for example, "str.split()" returns a list, while "str.split" (without parentheses) returns a method object. However, parenthesis in Ruby is always optional, so this road is blocked.

Well, how about "object.\method" style? You can type "str.\split" to get "str.method(:split)".

The reasons are:

1. It makes people happy, writing less code, and no harm to readability.

2. "\" is not a frequently used token. It can only be used in strings, regexps (or any else?). I think using this token leads to no ambiguity.

3. "\" is like Perl's referrence symbol. In Perl, you can use "$f = \&func;" to make a referrence to the function. In Ruby, "&" seems unnecessary.

4. It enhances the consistency of the language syntax. There are two correlative methods "Object#send" and "Object#method", but...

    str.send(:split)       == str.split
    str.method(:split)   == ???????

If adding this new syntax, it looks more pretty:
  
    str.method(:split)   == str.\split



-- 
http://redmine.ruby-lang.org