Calling method from another class

K

Kostas Lps

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.
 
D

David Masover

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

7stud --

Kostas said:
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.
 
7

7stud --

7stud said:
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.
 
L

louposk

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

Nilesh Trivedi

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
 
D

David Masover

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.
 
D

David Masover

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.
 
R

Rick DeNatale

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.

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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
T

timr

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.

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
 
R

Rick DeNatale

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
=A0def printA
=A0 =A0puts =3D "Class A method"
=A0end
end

class B
=A0def printB
=A0 =A0puts "Class B method"
=A0end
end

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

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.


--=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
 
D

Don Hill

[Note: parts of this message were removed to make it a legal post.]

Why not just use inheritance or did I miss something

class A < B
def printA
puts = "Class A method"
end
end


A.new.printB
 
7

7stud --

Kostas said:
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.
 
R

Rick DeNatale

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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top