Hi --

On Sat, 8 Feb 2003 nobu.nokada / softhome.net wrote:

> Hi,
>
> At Sat, 8 Feb 2003 11:30:25 +0900,
> dblack / candle.superlink.net wrote:
> > > > This has possible implications for other classes:
> > > >
> > > >   candle:~/hacking/ruby$ irb --simple-prompt
> > > >   >> class A < Array; end; A.new.replace([1,2,3]).class
> > > >   => A
> > > >   >> class A < Array; end; A.new.replace([1,2,3]).map {|x| x}.class
> > > >   => Array
> > >
> > > Enumerable#map always returns Array regardless its receiver.
> >
> > Right -- I'm just wondering why the "regardless-of-receiver" principle
> > operates here but not with String#scan and others.  (And with
> > Regexp#match it's the class of the argument, not the receiver, that
> > determines the return value's class.)
>
> In this case, the returned object isn't concerned with the
> receiver.  Enumerable methods return Array incidentally.  OTOH,
> String#[], #scan and so on return the receivers' subcomponent.

OK, but why is a subcomponent always (logically) the same class as the
original?  See my Name/String example.  Here's another example:

  class FiveLetterWord < String
    def initialize(s)
      raise ArgumentError, "Wrong length word" unless s.size == 5
      super
    end

    def vowels
      scan(/[aeiou]/i)
    end
  end

  f = FiveLetterWord.new("black")
  v1 = f.vowels[0]
  p v1.class                      # FiveLetterWord
  p v1.size                       # 1

I guess the full OO thing to do would be to have a separate class
FiveLetterWord::Vowel.  But in the absence of that, I can't help
feeling that a sub-component of FiveLetterWord should be a String.

> I also sometimes want ways to override the class of returned
> object, but it's another story.

Sounds interesting -- let's hear it :-)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav