Issue #5364 has been updated by Joey Zhou.


Well, Proc instances and Method instances are both closures. To obtain a closure, the Ruby idiom is almost from a block, not very often from a method. But in some situation, creating a closure from a method is more natural.

For example, if I want this:

    ['file1', 'file2', 'file3'].map {|f| open(f) }

how about:

    ['file1', 'file2', 'file3'].map &\open

and:
    str = "abcdabcdabcd"
    %w(a b c d).map {|sep| str.split(sep)}

how about:

    %w(a b c d).map &str.\split


Of course, every Method instance can be converted to a Proc object with Method#to_proc.

    str.method(:split)

is somewhat like:

    lambda {|*args| str.send(:split, *args) }

but I'm afraid it's verbose to write and read.



I've just begun to learn Python recently, and find some interesting differences between the two languages.

Python's lambda is trivial, only one line code is allowed to create a lambda. However, its functions are first class objects, so that can be assigned to another variable easily.

Python seems mainly using objects from function to do some functional programming thing, while Ruby seems mainly using objects from blocks.

    >>> a = lambda n: n + 1
    >>> a
    <function <lambda> at 0x011F6170>
    >>> def foo(n):
            return n + 1
    >>> b = foo
    >>> b
    <function foo at 0x011F2670>

whether from a lambda, or from a function, the returnning objects are both functions.

But in Ruby, if we do the similar thing, a will be a Proc instance, and b will be a Method instance.

Ruby distinguishes Proc and Method, but seems not paying much attention to Method? You can obtain a closure from a method, but it seems not Ruby idiom?

Joshua Ballanco and Yusuke Endoh san remind me the deeper subject: what's the future plan about the relationship between Proc and Method?
----------------------------------------
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