# Initialize not being called on objects created from literals

**URL:** <https://www.ruby-forum.com/t/initialize-not-being-called-on-objects-created-from-literals/168886>\
**Category:** Ruby\
**Created:** [May 27, 2009, 2:26am UTC](https://www.ruby-forum.com/t/initialize-not-being-called-on-objects-created-from-literals/168886 "2009-05-27T02:26:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [May 27, 2009, 2:26am UTC](https://www.ruby-forum.com/t/initialize-not-being-called-on-objects-created-from-literals/168886/1 "2009-05-27T02:26:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [May 27, 2009, 6:32am UTC](https://www.ruby-forum.com/t/initialize-not-being-called-on-objects-created-from-literals/168886/2 "2009-05-27T06:32:40Z")

</div>

On Wed, May 27, 2009 at 8:26 AM, Oliver S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”\>

see relation bw initialize and new

> 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?

try defining a method, eg

> > class Object  
> > def foo  
> > “bar”  
> > end  
> > end  
> > =\> nil  
> > 1.foo  
> > =\> “bar”  
> > //.foo  
> > =\> “bar”

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [May 27, 2009, 8:11am UTC](https://www.ruby-forum.com/t/initialize-not-being-called-on-objects-created-from-literals/168886/3 "2009-05-27T08:11:04Z")

</div>

On 27.05.2009 02:26, Oliver S. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [May 27, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/initialize-not-being-called-on-objects-created-from-literals/168886/4 "2009-05-27T14:55:43Z")

</div>

On Wed, May 27, 2009 at 2:10 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> invocation.  
> This is true, see below
> 
> # 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.

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 :old\_initialize, :initialize  
attr\_reader :boo

def initialize(\*args, &b) # !\> redefining Object#initialize may  
cause infinite loop  
puts “Object#initialize”  
old\_initialize(\*args, &b)  
@boo = “who”  
end  
end

puts “calling String.new”  
s1 = String.new  
puts “back”  
puts “s1.boo is #{s1.boo.inspect}”  
puts “making String#initialize call super”  
class String  
alias\_method :old\_initialize, :initialize  
def initialize(\*args, &b)  
super  
puts “String#initialize”  
old\_initialize(\*args, &b)  
end

end

puts “calling String.new”  
s2 = 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.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [May 27, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/initialize-not-being-called-on-objects-created-from-literals/168886/5 "2009-05-27T22:08:10Z")

</div>

Thanks Rick. I poked around a bit more after the last few posts made  
here and couldn’t find an answer. I started looking at the Ruby’s C code  
but couldn’t really get any useful information from it. Despite this I’m  
fairly confident what I wanted to do can’t be done. But I managed to  
work around it in the end with a solution that was actually surprisingly  
simple.

For those interested to know what I was doing I was trying to implement  
a layer for prototype-based OO in Ruby. Didn’t really work though. As  
far as I know (and please correct me if I’m wrong) Ruby doesn’t allow  
you to call a method redefining the meaning of self for the purpose of  
the call (e.g. apply() in JS) and without this feature you can’t really  
implement this layer in any useful way.

---

<div class="post-metadata">

**Author:** ![ole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/fbc32d/32.png) [@ole](https://www.ruby-forum.com/u/ole)\
**Post date:** [May 28, 2009, 12:04am UTC](https://www.ruby-forum.com/t/initialize-not-being-called-on-objects-created-from-literals/168886/6 "2009-05-28T00:04:05Z")

</div>

On 27.05.2009 22:08, Oliver S. wrote:

> For those interested to know what I was doing I was trying to implement  
> a layer for prototype-based OO in Ruby. Didn’t really work though.

Please correct me if I am wrong, but you seem to mean

> **[Prototype-based programming](https://en.wikipedia.org/wiki/Prototype-based_programming)**
>
> Prototype-based programming is a style of object-oriented programming in which behaviour reuse (known as inheritance) is performed via a process of reusing existing objects that serve as prototypes. This model can also be known as prototypal, prototype-oriented, classless, or instance-based programming.
> Prototype-based programming uses the process generalized objects, which can then be cloned and extended. Using fruit as an example, a "fruit" object would represent the properties and functionali...

Would this approximate what you’re after?

prototype = Object.new  
class \<\<prototype  
def foo  
printf “%p %p\n”, self, self.class  
end  
end  
o1 = prototype.clone  
o2 = prototype.clone  
o1.foo  
o2.foo

> As  
> far as I know (and please correct me if I’m wrong) Ruby doesn’t allow  
> you to call a method redefining the meaning of self for the purpose of  
> the call (e.g. apply() in JS) and without this feature you can’t really  
> implement this layer in any useful way.

#instance\_eval does exactly that: it temporarily sets self to point to a  
particular instance.

But it seems apply() in JS world is used to do something else

[http://www.devguru.com/Technologies/ecmascript/quickref/apply.html](http://www.devguru.com/Technologies/ecmascript/quickref/apply.html)

Basically you would need it to call “super class” methods. A few  
solutions come to mind. For construction you can do

def base  
o = Object.new  
class \<\<o  
def base\_meth  
end  
end  
o  
end

def derived  
o = base

# alt: prototype.clone

class \<\<o  
def derived\_meth  
end  
end  
o  
end

If your search the archives I am pretty sure you’ll find something about  
prototype based OO in Ruby.

Kind regards

robert
