to_proc

B

Brian Buckley

------=_Part_2806_28837954.1141754946901
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D> [2, 0, 3] but this works fin=
e

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?

------=_Part_2806_28837954.1141754946901--
 
M

Matthew Moss

I get the error "wrong argument type Symbol (expected Proc)
(TypeError)", not undefined method.
 
D

dblack

Hi --

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?

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


David

--
David A. Black ([email protected])
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
 
L

Logan Capaldo

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?

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top