[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Class in a Class problem

denize.paul

4/3/2009 1:35:00 AM

My intent is to create a class that has access to call a method in an
instantiation of another class

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()
6 Answers

Phlip

4/3/2009 1:55:00 AM

0

denize.paul@gmail.com wrote:

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

> class A
> class B
> def initialize()
> puts "b init"
> print_stuff()
> end
> end
>
> def B
> return B
> end

def B(*args)
@b ||= B.new(self, *args)
end

> def print_stuff()
> puts "here"
> end
> end

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


Phlip

4/3/2009 2:02:00 AM

0

denize.paul@gmail.com wrote:

> - I would have a world of more hurt trying...

You have only a _leetle_ bit more to learn about Ruby, Young Grasshopper!

(-:

matt

4/3/2009 4:02:00 AM

0

<denize.paul@gmail.com> wrote:

> My intent is to create a class that has access to call a method in an
> instantiation of another class
>
> 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()

Maybe I'm reading your question wrong, but the concept "has access to
call" is misleading, it seems to me, since there is never lack of access
in Ruby. So what you're really asking, I'm guessing, is that when a B
instance is produced by an A instance, it should point to that A
instance. We can do that without even giving the B class a name:

class A
def b
Class.new do
def initialize(a)
@a = a
end
attr :a
end.new(self)
end
# other stuff that an A knows how to do
end

So now you say:

a = A.new
b = a.b
puts (b.a == a) #=> true

So we see that b's a is indeed pointing at the correct instance. So now
you can say

b.a.print_stuff

or anything else you like. Is that the spirit of what you're after?

m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Robert Klemme

4/3/2009 6:48:00 AM

0

On 03.04.2009 06:02, matt neuburg wrote:
> <denize.paul@gmail.com> wrote:
>
>> My intent is to create a class that has access to call a method in an
>> instantiation of another class
>>
>> 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()
>
> Maybe I'm reading your question wrong, but the concept "has access to
> call" is misleading, it seems to me, since there is never lack of access
> in Ruby. So what you're really asking, I'm guessing, is that when a B
> instance is produced by an A instance, it should point to that A
> instance. We can do that without even giving the B class a name:
>
> class A
> def b
> Class.new do
> def initialize(a)
> @a = a
> end
> attr :a
> end.new(self)
> end
> # other stuff that an A knows how to do
> end

You are creating new classes all the time. I don't think this is a good
idea.

I am not sure what the OP really wants, if I'm not mistaken he wants to
prevent creation of B instances outside of A and also make sure there is
a 1:1 relationship between an A and a B instance.

First of all, the question is: why are they separated? We can't answer
that one from those abstract classes presented.

Hiding of class B for the outside could be done like this:

class A
@b = Class.new do
def print_stuff
puts "hoho"
end
end

def initialize
@b = self.class.instance_variable_get("@b").new
@b.print_stuff
end
end

a = A.new
p a

But a) this is not really hidden and b) this looks quite ugly.

Paul, what are you trying to accomplish?

Kind regards

robert

matt

4/3/2009 6:05:00 PM

0

Robert Klemme <shortcutter@googlemail.com> wrote:

> > class A
> > def b
> > Class.new do
> > def initialize(a)
> > @a = a
> > end
> > attr :a
> > end.new(self)
> > end
> > # other stuff that an A knows how to do
> > end
>
> You are creating new classes all the time. I don't think this is a good
> idea.

I'm not sure what you mean about new *classes*. It is certainly true
that I didn't do anything prevent the user from calling "b" twice on the
same A instance, thus getting two different objects pointing at that
instance:

a = A.new
b = a.b # b is an instance whose "@a" points at "a"
bb = a.b # bb is a different instance whose "@a" points at "a"

But that's easy to solve if desired. Obviously the real problem here is
that I couldn't fathom what the OP was actually trying to *do*. m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Robert Klemme

4/3/2009 6:32:00 PM

0

On 03.04.2009 20:05, matt neuburg wrote:
> Robert Klemme <shortcutter@googlemail.com> wrote:
>
>>> class A
>>> def b
>>> Class.new do
>>> def initialize(a)
>>> @a = a
>>> end
>>> attr :a
>>> end.new(self)
>>> end
>>> # other stuff that an A knows how to do
>>> end
>> You are creating new classes all the time. I don't think this is a good
>> idea.
>
> I'm not sure what you mean about new *classes*.

There is a Class.new in your definition of method b which means, every
time the method is invoked a new anonymous class is created - regardless
whether b is invoked several times on one or multiple instances.

> It is certainly true
> that I didn't do anything prevent the user from calling "b" twice on the
> same A instance, thus getting two different objects pointing at that
> instance:
>
> a = A.new
> b = a.b # b is an instance whose "@a" points at "a"
> bb = a.b # bb is a different instance whose "@a" points at "a"
>
> But that's easy to solve if desired. Obviously the real problem here is
> that I couldn't fathom what the OP was actually trying to *do*. m.

I had similar difficulties. That's why I asked.

Kind regards

robert