On 27.05.2009 02:26, Oliver Saunders wrote:
> Initialize doesn't appear to get called.
> 
> class Object
>   def initialize
>     @foo = 'bar'
>   end
>   attr_reader :foo
> end
> 
> a = Object.new # => #<Object:0x54c964 @foo="bar">
> a.foo # => "bar"
> 'string'.foo # => nil
> //.foo # => nil
> 
> I want to have //.foo and 'string.foo give me "bar" as they should. How
> can I do that?

A class needs to explicitly invoke #initialize of the super class.  I 
guess, since Object does not have any members by default String will not 
do the invocation.

class A
   def initialize
     puts "A"
   end
end

class B < A
   def initialize
     super
     # or super() in this case
     puts "B"
   end
end

Having said that, it's generally not a too good idea to mess with built 
in classes - even though you can.  But you may produce unwanted side 
effects.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/