From: David A. Black on
Hi --

On Sun, 8 Aug 2010, Warren Ferguson wrote:

> I'm stil mystified why substituting the expression for v into
> Bitvector.new(v) fails. Is it because Ruby optimizes that complex
> expression by realizing the expression is of class Array and Bitvector
> is a subclass of array?

No; it's because you weren't doing what you think you were doing :-)

Here are the two versions:

Bitvector.new(self.reverse.split(//)).map{|k| k.to_i(2)}

v = self.reverse.split(//).map{|k| k.to_i(2)}
Bitvector.new(v)

In the first one, you're calling map on a Bitvector object. In essence
it's this:

bv = Bitvector.new(self.reverse.split(//))
return bv.map{|k| k.to_i(2)}

In the second one, you're doing the map operation first, and then
sending the result of that operation to the BV initializer.

Move the parentheses around in the first one and you'll see what I mean:

Bitvector.new(self.reverse.split(//).map{|k| k.to_i(2)})


David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally
Compleat Philadelphia, PA, October 1-2, 2010
Rubyist http://www.compleatrubyist.com

From: Warren Ferguson on
Give me a dope slap.
I never saw the incorrect closing paren :-)
I must have added it, in the incorrect position,
when the parser complained.

Thanks again.
--
Posted via http://www.ruby-forum.com/.