From: "David A. Black" Date: 2010-08-08T09:05:01+09:00 Subject: Re: question about sub-class of array 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