# Define objects from classes in different files

**URL:** <https://www.ruby-forum.com/t/define-objects-from-classes-in-different-files/155917>\
**Category:** Ruby\
**Created:** [December 18, 2008, 6:48am UTC](https://www.ruby-forum.com/t/define-objects-from-classes-in-different-files/155917 "2008-12-18T06:48:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [December 18, 2008, 6:48am UTC](https://www.ruby-forum.com/t/define-objects-from-classes-in-different-files/155917/1 "2008-12-18T06:48:42Z")

</div>

Could someone help me with this little problem?

I have a file called fruit2.rb which contains:

class Fruit2  
def taste  
“sweet”  
end  
end

I want to use this class in a different file called fruit1.rb which  
contains:

require “fruit2”  
class Fruit1  
def color  
“red”  
end  
end

apple = Fruit1.new  
p apple.color  
apple = Fruit2.new  
p apple.taste

I know the way I’m creating my apple object is wrong because I  
over-wrote apple when I assigned Fruit2.new. What I want to do is have a  
way to call attributes of a single object from different files. In my  
application, I have so many methods, I want them in different files, but  
want to use them on a single object. Is there an easy way to do this?  
–thank you!

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [December 18, 2008, 6:53am UTC](https://www.ruby-forum.com/t/define-objects-from-classes-in-different-files/155917/2 "2008-12-18T06:53:39Z")

</div>

I understand that I could do

class Fruit1 \< Fruit2

but what if I have methods I want to use from many files? This  
inheritance only works for one?

Should I resort to modules?

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [December 18, 2008, 9:08am UTC](https://www.ruby-forum.com/t/define-objects-from-classes-in-different-files/155917/3 "2008-12-18T09:08:08Z")

</div>

Alle Thursday 18 December 2008, Jason L. ha scritto:

> I want to use this class in a different file called fruit1.rb which  
> p apple.color  
> apple = Fruit2.new  
> p apple.taste
> 
> I know the way I’m creating my apple object is wrong because I  
> over-wrote apple when I assigned Fruit2.new. What I want to do is have a  
> way to call attributes of a single object from different files. In my  
> application, I have so many methods, I want them in different files, but  
> want to use them on a single object. Is there an easy way to do this?  
> –thank you!

I’m not sure I understand correctly what you mean. In ruby, you can  
re-open a  
class after it’s been defined. This means you can put the taste method  
in one  
file and the color method in another one, both in the same class:

#in fruit2.rb  
class Fruit  
def taste  
“sweet”  
end  
end

# in fruit1.rb

class Fruit  
def color  
“red”  
end  
end

apple = Fruit.new  
puts apple.taste  
puts apple.color

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [December 18, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/define-objects-from-classes-in-different-files/155917/4 "2008-12-18T17:30:40Z")

</div>

I didn’t know you could re-open classes like that. I’ve done this in  
irb, but I didn’t realize this for actual files. This is the solution I  
need. Thanks!
