Symbol#to_proc Exonerated #
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.
Tesseract
Good riddance to the -> style blocks.
riffraff
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__
?daigo
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.
josh
Nice. I’ve been enjoying Symbol#to_proc in ActiveSupport. Will moving the implementation into C code improve the performance?
Ezra
w00t for multiple * splats in a def
MenTaLguY
I will not mourn stabby blocks, nor
;;
.rosejn
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.
Nicholas Wright
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.
Dave Burt
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.
omer
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.
omer
Hmmmph, seems this idea has already been submitted as an RCR and rejected.