[[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.
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.