Forum: Ruby to_proc

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9a46755c817121b0571a563e5ab02090?d=identicon&s=25 Brian Buckley (brian)
on 2006-03-07 19:11
(Received via mailing list)
require 'facet/symbol/to_proc'

[[1,2], [], [1,2,3]].map(&:length)  #line 1 Blows up re undefined method
length for Fixnum?

["ab", "", "abc"].map(&:length)  #line 2 => [2, 0, 3]  but this works
fine

Why does the first line fail by trying to call method length on Fixnum?
Shouldn't it be calling length on Array (which is defined) and thus
return
[2,0,3] ?  Note the next line behaves as I'd expect, but for Strings.

What's happening?
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-03-07 19:24
(Received via mailing list)
I get the error "wrong argument type Symbol (expected Proc)
(TypeError)", not undefined method.
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-03-07 19:24
(Received via mailing list)
Ack, nevermind. I'm a silly person, ignore me.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-07 19:36
(Received via mailing list)
Hi --

On Wed, 8 Mar 2006, Brian Buckley wrote:

>
> What's happening?

I'm only guessing but could it be flattening the outer array?


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-07 22:12
(Received via mailing list)
On Mar 7, 2006, at 1:09 PM, Brian Buckley wrote:

> Fixnum?
> Shouldn't it be calling length on Array (which is defined) and thus
> return
> [2,0,3] ?  Note the next line behaves as I'd expect, but for Strings.
>
> What's happening?

I figured it out! I figured it out!

theorizing that to_proc was implemented thusly:

class Symbol
     def to_proc
           lambda { |obj, *args| obj.send(self, *args) }
     end
end

which would be fine, except, ba bum bum! you have an array of arrays!
map or  each or someone is passing it via yield which is
sending :length.to_proc.call two args which turns into 1.send
(:length, 2)
This topic is locked and can not be replied to.