hoodwink.d enhanced


Symbol#to_proc Exonerated #

by why in cult

I guess RubyKaigi2006 is proving to be a nice hackathon, cause Ruby CVS is suddenly checkins aplenty. Of particular note is the inclusion of Symbol#to_proc, a long-standing feat of clever quacking we’ve all come to adore.

 Sun Jun 11 04:38:20 2006  Nobuyoshi Nakada  <nobu@ruby-lang.org>
    * object.c (sym_to_proc): imported Symbol#to_proc from ActiveSupport.

 Sat Jun 10 15:12:29 2006  NAKAMURA Usaku  <usa@ruby-lang.org>
   * eval.c (rb_f_method_name, rb_f_callee_name): new functions.
      new global method `__method__' and `__callee__'.

Over the last few days, Matz has also backed out a lot of the experimental syntax out of HEAD (the ;; terminator, the -> style blocks) while adding flexible argument lists (multiple splats, additional arguments after the splat.)

Mauricio has more of the particulars, how Proc#yield works, cites some lineage on the above.

said on 10 Jun 2006 at 19:51

Good riddance to the -> style blocks.

said on 10 Jun 2006 at 20:09

I’d have preferred UnboundMethod#call and a quick sintax for Class#instance_method to write something like [john,paul,ringo,george].sort_by &Person.age but I think it is cool anyway :)

Oh, did I say how much I love __method__ and __callee__ ?

said on 10 Jun 2006 at 22:53

Matz commited a lot early morning of the day. Some guys told matz at the session that it did not compile and he should commit separate features not at a time.

said on 11 Jun 2006 at 11:57

Nice. I’ve been enjoying Symbol#to_proc in ActiveSupport. Will moving the implementation into C code improve the performance?

said on 11 Jun 2006 at 21:31

w00t for multiple * splats in a def

said on 11 Jun 2006 at 21:51

I will not mourn stabby blocks, nor ;;.

said on 12 Jun 2006 at 06:18

So they got rid of the ->(){} syntax, but was it replaced by an alternative? This is the addition to support default argument values in blocks, right? I hope that is still in the mix somewhere.

said on 12 Jun 2006 at 10:39

I’ll be the first to celebrate the death of stabby blocks. I think keeping ruby out of the ‘line noise’ catagory should be a goal moving forward. The -> and ;; stuff would do a terrible injustice to the elegant look of ruby.

said on 12 Jun 2006 at 20:10

Symbol#to_proc allows this:

[john,paul,ringo,george].sort_by &:age

which is polymorphic—it will call john.age, paul.age, etc, whether or not they are Person instances.

Now, let’s say we make klass::method mean klass.instance_method(:method) (a little crazy, since UnboundMethods aren’t much use), and use it thus:

[john,paul,ringo,george].sort_by &Person::age

This is still pretty terrible, riffraff. UnboundMethod#call would have to accept an extra parameter to re-bind the method to, and in the example, re-bind to each object in turn, creating a method object for each object.

This is what procs and blocks are for. Instance methods are meant to operate on instances, not be executed in the context of random other objects.

said on 28 Jun 2006 at 06:40

I’ve been wondering – is there a way to make symbol#to_proc pass parameters to the target object? If possible, this could improve the shorthand style, as in: (1..10).select(:<, 5)


see this post by Avi Bryant: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/23519

I realize I could get this to work by writing a core extension that modifies the Enumerable’s select\collect\etc, but I was wondering if Symbol#to_proc can do it for me.

said on 28 Jun 2006 at 07:04

Hmmmph, seems this idea has already been submitted as an RCR and rejected.

12 Jul 2010 at 22:15

* do fancy stuff in your comment.