From: James Coglan Date: 2009-07-08T17:54:28+09:00 Subject: Re: Symbol#to_proc with arguments --001636c5a82fcfb37a046e2de2e5 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 2009/7/8 Max Williams > hey folks > > I've often wondered if i can use Symbol#to_proc with arguments - reading > Dave Thomas's explanation on this page > http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html it > looks like i would be able to do this: > > >> array = %w(apple banana peach plum pear) > => ["apple", "banana", "peach", "plum", "pear"] > >> array.collect(&:split, 'l') > SyntaxError: compile error > (irb):12: syntax error, unexpected ',', expecting ')' > array.collect(&:split, 'l') > ^ > (irb):12: syntax error, unexpected ')', expecting $end > from (irb):12 > > But it fails as you can see. Trying to fully grasp what's going on with > Symbol#to_proc makes my head hurt a little bit (it is only 9.16am here) > and i'm clearly not getting it. Can anyone explain? The block argument (prefixed with '&') must be the last argument in the list when calling a method, so passing 'l' in your example is not valid syntax. You could rewrite Symbol#to_proc to take arguments, and call #to_proc explicitly when calling #collect. class Symbol def to_proc(*args) lambda { |o| o.__send__(self, *args) } end end %w[apple banana peach plum pear].collect &:split.to_proc('l') #=> [["app", "e"], ["banana"], ["peach"], ["p", "um"], ["pear"]] -- James Coglan http://jcoglan.com --001636c5a82fcfb37a046e2de2e5--