Issue #5364 has been updated by Thomas Sawyer.


It's just a convenience thing, so I think it's not possible to prove a "real use case" as such. And as only a static syntax it's probably not a convenience worth the bother. However it might be more understandable if it could also be used dynamically, e.g.

  m = :split
  str\:m

Syntax not withstanding. I suppose if this were to be done, the nicest syntax would be:

  str#m

and

  str#:m

In which case comments would require a space before the hash mark.

But, I think this hints at a larger matter that a future version of Ruby could really use -- real meta-programming functions. As things stand Ruby's meta-programming methods are fragile. Hence the need for things like __id__ and __send__. Likewise, if #method is overwrite by some class, it could spell a bad day for some meta-code. As to the syntax for real meta-programming functions, I'm not sure. Perhaps a "global meta-izing syntax" like:

  $(object).id
  $(object).send(...)

And then perhaps #[] could get a method:

  $(object)[:foo] 

On the other hand, perhaps $ could be used as a "meta-message" device:

  object$id
  object$send(...)
  object$:foo

In any case, one way or the other a more robust address of meta-programming would be a good thing.


----------------------------------------
Feature #5364: How about new syntax: "object.\method" returns a Method instance?
http://redmine.ruby-lang.org/issues/5364

Author: Joey Zhou
Status: Feedback
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