« More Music... Breaking the Logjam | Main | Time for Cocoa »

September 09, 2008

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e20105349b8cd6970c

Listed below are links to weblogs that reference Fun with Procs in Ruby 1.9:

Comments

Russell Jones

I love it, thanks for the demo :)

yhvd

Do you know if Matz have considered the stuff in the followup post that introduces usage of === for select/any/all etc ?

This instantly rings true for me, for example:

%w(ape ball monkey_wrench mall).select /all/

Farrel

Holy cow! I never thought that Proc#call being aliased to Proc#=== would ever be taken seriously!

Bob Aman

I love the concept, but I hate the implementation.

Why not just have case statements either send :=== or :call?

Bradly Feeley

I never knew of currying. Very cool.

Linh Nguyen

This kind of stuff is what making programming with Ruby so fun. Thanks for the demo.

AndrewO

Wow. Nice right up!

Some questions though: why aren't Proc's curry-able by default? Performance?

Is the curried Proc actually partially evaluated or is it some kind of proxy object that's saved the argument and only actually calls the Proc once it has all of them?

Emmanuel Gomez

I'm loving Proc#=== and the examples you build with it, not least because I'm going to take and integrate it (and your example) into a little lib I'm building called Clockwork (http://github.com/emmanuel/clockwork/tree/master ).

Your example is quite close to what Clockwork does, though without the benefit of Proc#===, thus far. Ironically, I started on Clockwork while in Bali with no internet access (wrote it out with a mechanical pencil in long hand), then came back to find this blog post! Some ideas arrive when their time has come, I suppose. But really, get outta my head, Dave! :)

Proc#=== is going to be very useful for things like Election Day--Tuesday after the first Monday in November.

Thanks, Dave!

Jason Zaugg

Andrew: To preserve backwards compatibility, Proc#[] has to throw a TypeError if too few arguments are provided.

irb(main):007:0> p = Proc.new do |a, b| a + b; end
=> #
irb(main):008:0> p[1, 2]
=> 3
irb(main):009:0> p[1]
TypeError: nil can't be coerced into Fixnum

Jason Zaugg

Or more precicely, pad out the argument list with nils to match the arity of the Proc:

irb(main):010:0> p = Proc.new do |a, b| b.class; end
=> #
irb(main):011:0> p[1, 2]
=> Fixnum
irb(main):012:0> p[1]
=> NilClass
irb(main):013:0> p = Proc.new do |*a| a.class; end
=> #
irb(main):014:0> p[1]
=> Array
irb(main):015:0> p.arity
-1

Aldric

Wow, is that some LISP that I observe? Sweet!

g.zhen.ning

Can I translate this article into Chinese? Of course, keep the original link and your name.

The comments to this entry are closed.

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now available
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.