# Calling method from another class

**URL:** <https://www.ruby-forum.com/t/calling-method-from-another-class/174317>\
**Category:** Ruby\
**Created:** [August 25, 2009, 12:11am UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317 "2009-08-25T00:11:37Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 25, 2009, 12:11am UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/1 "2009-08-25T00:11:37Z")

</div>

Hi guys,  
i have 2 classes say class A and class B. How can the A object that i’ll  
create to call a class B method???  
Say we have this code:

class A  
def printA  
puts = “Class A method”  
end  
end

class B  
def printB  
puts “Class B method”  
end  
end

I want to make something like this:  
a = A.new  
a.printB

I this possible someway??? I dont want to use modules.

Thanx in advance  
Kostas L.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 25, 2009, 3:36am UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/2 "2009-08-25T03:36:09Z")

</div>

On Monday 24 August 2009 05:12:44 pm Kostas L. wrote:

> I want to make something like this:  
> a = A.new  
> a.printB

Use modules or inheritance.

> I this possible someway???

Not the way you’ve defined those classes. If B was either a superclass  
of A, or  
a module mixed into A, you could use unbound methods, but it’s not, so  
you  
can’t.

> I dont want to use modules.

Why not?

This is exactly why modules exist – to solve exactly, precisely, the  
problem  
you’ve outlined.

Ask yourself this question: Do the methods you want out of B make any  
sense at  
all in A, or in any other class? If so, why not make it a module? If  
not, you  
may want to do some sort of delegation – that is:

class A  
def initialize  
@b = B.new  
end  
def printB  
@b.printB  
end  
end

Does that answer your question?

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 25, 2009, 5:40am UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/3 "2009-08-25T05:40:54Z")

</div>

Kostas L. wrote:

> Hi guys,  
> i have 2 classes say class A and class B. How can the A object that i’ll  
> create to call a class B method???  
> Say we have this code:
> 
> class A  
> def printA  
> puts = “Class A method”  
> end  
> end
> 
> class B  
> def printB  
> puts “Class B method”  
> end  
> end
> 
> I want to make something like this:  
> a = A.new  
> a.printB
> 
> I this possible someway???

a’s available methods are:

1. The methods defined in A.
2. The methods defined in the classes A inherits from.
3. The methods that A mixes in.
4. The object specific “singleton” methods of a.

The method B#printB is not one of those.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 25, 2009, 5:44am UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/4 "2009-08-25T05:44:12Z")

</div>

7stud – wrote:

> a’s available methods are:
> 
> 1. The methods defined in A.
> 2. The methods defined in the classes A inherits from.
> 3. The methods that A mixes in.
> 4. The object specific “singleton” methods of a.

..and I guess:

3b) The methods that were mixed into the classes A inherits from.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 25, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/5 "2009-08-25T16:00:53Z")

</div>

Hi guys,  
thank you both for your answers! So the only way to do this is with  
modules i guess…  
You both helped me clear the black clouds inside my mind! 🙂

Something else. A mixin and a module is the same thing??

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 25, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/6 "2009-08-25T21:02:42Z")

</div>

Kostas L. wrote:

> Hi guys,  
> thank you both for your answers! So the only way to do this is with  
> modules i guess…  
> You both helped me clear the black clouds inside my mind! 🙂
> 
> Something else. A mixin and a module is the same thing??

A mixin is a module, but a module isn’t necessarily a mixin. On the one  
hand, a module can be written as a general purpose mixin for various  
classes. On the other hand, a module can be used as a namespace to  
prevent name clashes with other methods.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 26, 2009, 5:17pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/7 "2009-08-26T17:17:31Z")

</div>

On Tuesday 25 August 2009 09:00:08 am louposk wrote:

> Something else. A mixin and a module is the same thing??

Maybe.

Modules can be mixed into classes, so a mixin is a module. However,  
modules  
can also be used for other things, like namespacing – not all modules  
are  
ever intended to be mixed in.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 26, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/8 "2009-08-26T17:43:53Z")

</div>

On Mon, Aug 24, 2009 at 6:12 PM, Kostas L.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I this possible someway??? I dont want to use modules.  
> Well, you seem to have lobbed this question into the list and gone  
> away, so it’s hard to discover whether you’re satisfied with the  
> answers so far, or if you’ve even seen them.

Nobody has asked this but I have to.

Why do you feel a need to do this? It’s like wanting to take the  
square root of a string, or uppercasing an integer.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [NameBright - Coming Soon](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:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 26, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/9 "2009-08-26T18:15:33Z")

</div>

On Tue, Aug 25, 2009 at 7:30 PM, louposk[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi guys,  
> thank you both for your answers! So the only way to do this is with  
> modules i guess…  
> You both helped me clear the black clouds inside my mind! 🙂
> 
> Something else. A mixin and a module is the same thing??

I have a related question. “Class” is a subclass of “Module”. Then why  
is it that I can include a Module but not another Class?

class A  
end

class B  
include A # Throws a TypeError: wrong argument type Class (expected  
Module)  
end

IIRC, there used to be a ruby plugin called evil\_ruby which allowed  
this. Why isn’t this supported natively in Ruby?

cheers  
nilesh  
[http://nileshtrivedi.in](http://nileshtrivedi.in)

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 26, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/10 "2009-08-26T17:50:20Z")

</div>

Why not just use inheritance or did I miss something

class A \< B  
def printA  
puts = “Class A method”  
end  
end

A.new.printB

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 26, 2009, 6:15pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/11 "2009-08-26T18:15:34Z")

</div>

On Aug 24, 3:12 pm, Kostas L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I this possible someway??? I dont want to use modules.
> 
> ## Thanx in advance Kostas L.
> 
> Posted viahttp://www.ruby-forum.com/.

Could you do this:

class A  
def putsFromA  
puts “hello from A”  
end  
end

class B  
def initialize(anA)  
@anA = anA  
end  
def putsFromB  
puts “hello from B”  
end  
def getAMessageFromB  
@anA.send :putsFromA  
end  
end

a = A.new  
b = B.new(a)  
b.getAMessageFromB

# \>\> hello from A

If you made class methods rather than instance methods you could get  
even closer to the desired syntax:

class A  
def self.putsFromA  
puts “hello from A”  
end  
end

class B  
def self.putsFromB  
puts “hello from B”  
end

def self.getAMessageFromB  
A.send :putsFromA  
end

end

B.getAMessageFromB

# \>\> hello from A

So modules would be one way, using the send method with an symbol  
representing the name of the method as an argument is another. I think  
it is closer to what you were hopping for originally. It basically  
making the B class a proxy for A, at least for one specified method  
(it receives a method call and forwards it to the actual target  
class).  
Tim

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 26, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/12 "2009-08-26T18:18:29Z")

</div>

On Tue, Aug 25, 2009 at 12:53 PM, David M.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> understand.  
> Class inherits the behavior to act as a namespace, and the ability to  
> hold instance methods from Module.

Classes can instantiate instances, Modules cannot.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [NameBright - Coming Soon](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:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 26, 2009, 6:17pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/13 "2009-08-26T18:17:54Z")

</div>

On Tuesday 25 August 2009 09:43:29 am Nilesh T. wrote:

> I have a related question. “Class” is a subclass of “Module”. Then why  
> is it that I can include a Module but not another Class?

You can include a module because you can include exactly modules, and  
nothing  
else – just as you can inherit from exactly classes, and nothing else.

I don’t really know why Class is a subclass of Module. I’m guessing  
there is  
some code shared between them, or maybe it makes other things easier to  
understand.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 27, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/14 "2009-08-27T17:31:11Z")

</div>

On Thu, Aug 27, 2009 at 1:59 AM, Kostas L.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So a module is a considered to be a class? Or is something completely  
> different??

Well in a classical sense Class is a subclass of Module, so someone  
who viewed the world as class=type hierarchies would say that a class  
was a module.

But there a things which modules can be used for which classes cannot  
particularly modules can be included in other modules and classes,  
classes cannot. So in that sense classes aren’t modules.

Classes can have instances, whereas modules cannot.

If you do think of classes as types and class inheritance as type  
inheritance, you will at times find counter examples like this in  
ruby. Inheritance in Ruby provides overrideable implementation  
decoupled from the structure of instance data because of the way Ruby  
dynamically finds and adds instance variables as methods need them.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [NameBright - Coming Soon](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:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 27, 2009, 7:58am UTC](https://www.ruby-forum.com/t/calling-method-from-another-class/174317/15 "2009-08-27T07:58:17Z")

</div>

So a module is a considered to be a class? Or is something completely  
different??

Rick Denatale wrote:

> On Tue, Aug 25, 2009 at 12:53 PM, David M.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > understand.  
> > Class inherits the behavior to act as a namespace, and the ability to  
> > hold instance methods from Module.
> 
> Classes can instantiate instances, Modules cannot.
> 
> –  
> Rick DeNatale
> 
> Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
> Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
> WWR: [NameBright - Coming Soon](http://www.workingwithrails.com/person/9021-rick-denatale)  
> LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
