My intent is to create a class that has access to call a method in an
instantiation of another class
Here be dragons. Have you considered the inheritance idea? If a class *needs* access to another class's private variables, then the functionality either belongs in the same class or a child class. Whilst this is technically legal, it really doesn't bode well. If you absolutely need Class A to have exactly one instantiation of Class B you might want to consider the Singleton design pattern for class B. Which in ruby is _really_ difficult to implement...
include singleton #(or something along those lines)
and for B to be a class variable of A (@@b = B.new) which, if they act like Java statics (and I think they do) the class variable has one instantiation able to be accessed from any object of the class.
e.g
class B
def initialize()
end
def to_s
"This is class #{class}"
end
end
class A
@@b = B.new()
def intitialize()
puts @@b
end
end
But in truth, you will have to be a little less generic to get a better answer. If you can give more details of what you're trying to achieve, then please do, and I'll try and give better help.
Michael
···
denize.paul@gmail.com wrote:
However my knowledge of Ruby classes (Ok classes in general) is
somewhat lackingThe example below is a LOT simplified But I need class B to call a
method within an instantiation of class A's.one alternative is to seperate the classes and then just do "b=B.new
(a)" but I think that looks really messy and
- I have then to check a bunch of stuff about the parameter a
- I would have a world of more hurt trying to ensure that only one B
instantiation pointed to each aCan anyone help? Or tell me that I am doing this all wrong.
Paul
-----
class A
class B
def initialize()
puts "b init"
print_stuff()
end
enddef B
return B
enddef print_stuff()
puts "here"
end
enda = A.new()
b = a.B.new()