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.
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?
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:
- The methods defined in A.
- The methods defined in the classes A inherits from.
- The methods that A mixes in.
- The object specific “singleton” methods of a.
The method B#printB is not one of those.
7stud – wrote:
a’s available methods are:
- The methods defined in A.
- The methods defined in the classes A inherits from.
- The methods that A mixes in.
- The object specific “singleton” methods of a.
…and I guess:
3b) The methods that were mixed into the classes A inherits from.
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??
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.
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.
On Mon, Aug 24, 2009 at 6:12 PM, Kostas L.removed_emai[email protected] 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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
On Tue, Aug 25, 2009 at 7:30 PM, louposk[email protected] 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
Why not just use inheritance or did I miss something
class A < B
def printA
puts = “Class A method”
end
end
A.new.printB
On Aug 24, 3:12 pm, Kostas L. [email protected] 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
On Tue, Aug 25, 2009 at 12:53 PM, David M.[email protected]
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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
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.
On Thu, Aug 27, 2009 at 1:59 AM, Kostas L.[email protected] 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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
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.[email protected]
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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale