From: Tobias Reif Date: 2001-09-21T19:20:54+09:00 Subject: [ruby-talk:21484] Re: chain methods inside and outside the class; how to intermingle mine and Strings' methods? Michael Neumann wrote: >># works, because h.upcase returns a HoX >>[...] >># doesn't work, because h.reverse returns a String >>[...] > Write a Delegator class that delegates all String methods to the internal-hold > String object, checks if the return value of it is a String, in which case you > create a new HoX object that holds the String internally. May I ask why String methods behave in such different ways? (Principle Of Least Surprise wise) For me, it was a surprise, and I can't see a reason. I can't just be inconsistency. And the solution seems to be complex for a beginner. Chaining string methods mixed with subclass methods would be easier to use if all String methods would return an object of the same type of the receiver. Here is the problem in short: ## in ruby 1.6.3 (2001-03-19) [i386-cygwin] class Foo < String end h = Foo.new 'bar' p h.upcase.type # -> Foo p h.reverse.type # -> String Why? Tobi -- Tobias Reif http://www.pinkjuice.com/myDigitalProfile.xhtml go_to('www.ruby-lang.org').get(ruby).play.create.have_fun http://www.pinkjuice.com/ruby/