A class needs to explicitly invoke #initialize of the super class. =A0I g= uess,
since Object does not have any members by default String will not do the
invocation.
This is true, see below
class A
=A0def initialize
=A0 =A0puts "A"
=A0end
end
class B < A
=A0def initialize
=A0 =A0super
=A0 =A0# or super() in this case
=A0 =A0puts "B"
=A0end
end
Having said that, it's generally not a too good idea to mess with built i= n
classes - even though you can. =A0But you may produce unwanted side effec=
ts.
But that's not the whole story. As the OP suspected, literals are
created by the parser without going through the normal initialization
route:
class Object
alias_method

ld_initialize, :initialize
attr_reader :boo
def initialize(*args, &b) # !> redefining Object#initialize may
cause infinite loop
puts "Object#initialize"
old_initialize(*args, &b)
@boo =3D "who"
end
end
puts "calling String.new"
s1 =3D String.new
puts "back"
puts "s1.boo is #{s1.boo.inspect}"
puts "making String#initialize call super"
class String
alias_method

ld_initialize, :initialize
def initialize(*args, &b)
super
puts "String#initialize"
old_initialize(*args, &b)
end
end
puts "calling String.new"
s2 =3D String.new
puts "s2.boo is #{s2.boo.inspect}"
puts "\"abc\".boo is #{"abc".boo.inspect}"
Produces the following
calling String.new
back
s1.boo is nil
making String#initialize call super
calling String.new
Object#initialize
String#initialize
s2.boo is "who"
"abc".boo is nil
untitled:5: warning: redefining Object#initialize may cause infinite loop
Note that warning pointing out a potential unwanted side effect.
--=20
Rick DeNatale
Blog:
http://talklikeaduck.denhaven2.com/
Twitter:
http://twitter.com/RickDeNatale
WWR:
http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn:
http://www.linkedin.com/in/rickdenatale