Issue #5364 has been updated by Joey Zhou.



How about this?

I want to create an enumerator from a method, the normal way is using the Object#to_enum method, if we add a method Method#to_enum, we can write in a clearer way. For example:


str.each_char # want an enumerator from this

  # normal  way  ==>  str.to_enum(:each_char)
  # clearer way  ==>  str.\each_char.to_enum

of course, str.each_char returns an enumerator itself, but some other methods are not.


Dir.glob("*.rb") {|f| } # if I want an enumerator, not an array:

  # normal  way  ==>  Dir.to_enum(:glob, "*.rb")
  # clearer way  ==>  Dir.\glob.to_enum("*.rb")


str.scan(/[abc]+/) {|s| }

  # normal  way  ==>  str.to_enum(:scan, /[abc]+/)
  # clearer way  ==>  str.\scan.to_enum(/[abc]+/)


array.sort {|a,b| }

  # normal  way  ==>  array.to_enum(:sort)
  # clearer way  ==>  array.\sort.to_enum

In my opinion, compare with the methods which take a method name as its argument, "object.\method" style is more readable.
----------------------------------------
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