# Override object initialization, add a filepath attribute

**URL:** <https://www.ruby-forum.com/t/override-object-initialization-add-a-filepath-attribute/113068>\
**Category:** Ruby\
**Created:** [August 27, 2007, 6:45am UTC](https://www.ruby-forum.com/t/override-object-initialization-add-a-filepath-attribute/113068 "2007-08-27T06:45:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 27, 2007, 6:45am UTC](https://www.ruby-forum.com/t/override-object-initialization-add-a-filepath-attribute/113068/1 "2007-08-27T06:45:15Z")

</div>

Is it possible to override initializations so that anytime an object is  
created there is a “filepath” attribute put on it?

maybe?

class Object  
def initialize  
super  
class \<\< self  
attr\_accessor :filepath  
end  
self.filepath = $0  
end  
end

See what i’m going for? Any ideas on how to get away with this?

Thanks.

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 27, 2007, 7:33am UTC](https://www.ruby-forum.com/t/override-object-initialization-add-a-filepath-attribute/113068/2 "2007-08-27T07:33:26Z")

</div>

On 27 Aug 2007, at 13:45, Aaron S. wrote:

> ```
> attr_accessor :filepath
> 
> ```
> 
> end  
> self.filepath = $0  
> end  
> end
> 
> See what i’m going for? Any ideas on how to get away with this?

Do you want the name of the file each object is defined in? I think  
you want **FILE** instead of $0 in that case since $0 is always the  
top level Ruby program.

Also there is no superclass for Object so super#initialize won’t work  
I think. This maybe gets closer - but still doesn’t actually work:

[alexg@powerbook]/Users/alexg/Desktop(55): cat main.rb  
require ‘foo’  
require ‘filepath’

f = Foo.new  
p f.filepath  
[alexg@powerbook]/Users/alexg/Desktop(56): cat foo.rb  
class Foo  
end  
[alexg@powerbook]/Users/alexg/Desktop(57): cat filepath.rb  
class Object  
attr\_accessor :filepath  
end

class Class  
alias :old\_new :new  
def new(\*args)  
result = old\_new(\*args)  
result.filepath = **FILE**  
result  
end  
end  
[alexg@powerbook]/Users/alexg/Desktop(58): ruby main.rb  
“./filepath.rb”

We would of course like it to output ‘foo.rb’ if I understand you right.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 27, 2007, 8:06am UTC](https://www.ruby-forum.com/t/override-object-initialization-add-a-filepath-attribute/113068/3 "2007-08-27T08:06:44Z")

</div>

Great, this will work perfectly. thanks again

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 27, 2007, 8:41am UTC](https://www.ruby-forum.com/t/override-object-initialization-add-a-filepath-attribute/113068/4 "2007-08-27T08:41:36Z")

</div>

On 27 Aug 2007, at 15:06, Aaron S. wrote:

> ## Great, this will work perfectly. thanks again
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

???

But it doesn’t work! In my ‘solution’ the filepath attribute is  
always set to ‘filepath.rb’ instead of the name of the file the  
object is defined in. It’s useless as far as I can see! Am I  
completely missing what you wanted?

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [August 27, 2007, 8:55am UTC](https://www.ruby-forum.com/t/override-object-initialization-add-a-filepath-attribute/113068/5 "2007-08-27T08:55:16Z")

</div>

Alex G. wrote:

> On 27 Aug 2007, at 15:06, Aaron S. wrote:
> 
> > ## Great, this will work perfectly. thanks again
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> ???
> 
> But it doesn’t work! In my ‘solution’ the filepath attribute is  
> always set to ‘filepath.rb’ instead of the name of the file the  
> object is defined in. It’s useless as far as I can see! Am I  
> completely missing what you wanted?
> 
> Alex G.
> 
> Bioinformatics Center  
> Kyoto University

OH yeah, sorry, I just assumed that would work. I am wanting the file  
path where the class is defined, not just the top level ruby script that  
is running… I’ll give this a shot tomorrow and see if I can figure it  
out based on what you’ve written.
