Class in a Class problem

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) :slight_smile:

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 lacking

The 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 a

Can 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
  end

  def B
    return B
  end

  def print_stuff()
    puts "here"
  end
end

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

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.