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?
···
On Monday 24 August 2009 05:12:44 pm Kostas Lps 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.
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 Aug 24, 3:12 pm, Kostas Lps <loup...@gmail.com> 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??? I dont want to use modules.
Thanx in advance
Kostas L.
--
Posted viahttp://www.ruby-forum.com/.
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.
···
On Mon, Aug 24, 2009 at 6:12 PM, Kostas Lps<louposk@gmail.com> 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??? I dont want to use modules.
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.
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??
On Tue, Aug 25, 2009 at 7:30 PM, louposk<louposk@gmail.com> 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??
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 Tuesday 25 August 2009 09:00:08 am louposk wrote:
Something else. A mixin and a module is the same thing??
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.
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 Tuesday 25 August 2009 09:43:29 am Nilesh Trivedi 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?
Class inherits the behavior to act as a namespace, and the ability to
hold instance methods from Module.
Classes can instantiate instances, Modules cannot.
···
On Tue, Aug 25, 2009 at 12:53 PM, David Masover<ninja@slaphack.com> wrote:
On Tuesday 25 August 2009 09:43:29 am Nilesh Trivedi 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.
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.
···
On Thu, Aug 27, 2009 at 1:59 AM, Kostas Lps<louposk@gmail.com> wrote:
So a module is a considered to be a class? Or is something completely
different??