The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Symbol#to_proc ... thy fearful symmetry

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
rodney ramdas

Posts: 66
Nickname: pinupgeek
Registered: Jun, 2006

Rodney Ramdas is a de-enterprised Ruby on Rails developer from the Netherlands.
Symbol#to_proc ... thy fearful symmetry Posted: Jun 19, 2006 5:59 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by rodney ramdas.
Original Post: Symbol#to_proc ... thy fearful symmetry
Feed Title: pinupgeek.com
Feed URL: http://feeds.feedburner.com/pinupgeek
Feed Description: A personal take on Ruby and Rails
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by rodney ramdas
Latest Posts From pinupgeek.com

Advertisement

Recently I reported on Nobuhiro Imai’s patch to ActiveSupport’s Symbol#to_proc. In Ruby 1.9 a similar Symbol#to_proc will be added. But on Ruby-Talk a discussion about the correctness of this method is happening right now (yes, we are live and direct). Eliot Temple provides a cool test case. Let me summarize, first here’s the current ActiveSupport (i.e. Rails) version:

irb(main):017:0> class Symbol
irb(main):018:1>   def to_proc
irb(main):019:2>     Proc.new { |obj, *args| obj.send(self, *args) }
irb(main):020:2>   end
irb(main):021:1> end

This will fail when #reverse-ing arrays. Turns out this is about the same way it works in the upcoming Ruby 1.9

[4,5,6], [7,8,9]].map &:reverse

Which brings us back to Nobuhiro-san’s patch that does make reversing work. He provides the following examples. This is his definition:

class Symbol
 def to_proc
   Proc.new{|*args| args.shift.__send__(self, *args)}
 end
end

Nobuhiro-san provides some inspiring examples:


>> [[1,2,3], [4,5,6], [7,8,9]].map(&:reverse)
=> [[3, 2, 1], [6, 5, 4], [9, 8, 7]]

This one is my favourite:

>> (1..10).inject(&:+)
=> 55

And here’s the famous one again:

>> {0=>"zero",1=>"one",2=>"two",3=>"three"}.sort_by(&:first).map(&:last)
=> ["zero", "one", "two", "three"]

What’s mind boggling is the minimal diff between the 2 implementations but the implications are deep:


ActiveSupport:Proc.new{ |obj, *args| obj.send(self, *args) }

Nobuhiro-san :Proc.new{ |*args| args.shift.__send__(self, *args)}

I’m only starting to understand this stuff, just barely, so correct me if I’m wrong. I have an instinctive feel of what’s going on but it’s hard to translate.

In the Rails version it looks like it’s assumed that Symbol#to_proc takes an array where it’s members are mapped as key, method pairs, hence *args.

You cannot reverse it since that would not make sense. In Noburio-san’s version to_proc takes anything it’s given ,doesn’t assume anything about it, and calls __send__ on itself. I need to try to make a graphical representation of this maybe that will help me to understand it. (I think __send__is used rather then Object#send to avoid some sort of name clashing or something).

At this point my brain is in a Gordian Knot and I’m gasping for air, I need to study this a bit more. If you can help, please comment. I will wash your car or camper if you can make me understand this.

Finally , here the verse that prompted the title for this post, fearful indeed.

TIGER, tiger, burning bright     
In the forests of the night,     
What immortal hand or eye     
Could frame thy fearful symmetry?

(by William Blake)

Read: Symbol#to_proc ... thy fearful symmetry

Topic: Data extraction for Web 2.0: Screen scraping in Ruby/Rails Previous Topic   Next Topic Topic: Single Table Inheritance in Ruby on Rails

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use