Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Nested classes

Reply
Thread Tools

Nested classes

 
 
Groleo Marius
Guest
Posts: n/a
 
      11-16-2005
hi list.

I have the following code:

#! /usr/bin/ruby


class B
def fun_b
p "B"
end
end

class A
b =3D B.new
end

a =3D A.new
a.b.fun_b

The part that bugs me is this error:
/test.rb:15: undefined method `b' for #<A:0xb7cafb24> (NoMethodError)

What is the way to code the above ideea, so that the last line would be cor=
rect.

I'm running ruby 1.8.3 (2005-09-21) [i486-linux].


--
Regards, Groleo!

# Use "Reply to All" on mailing lists.
# touch universe
# chmod +x universe
# ./universe


 
Reply With Quote
 
 
 
 
Brian Schröder
Guest
Posts: n/a
 
      11-16-2005
On 16/11/05, Groleo Marius <> wrote:
> hi list.
>
> I have the following code:
>
> #! /usr/bin/ruby
>
>
> class B
> def fun_b
> p "B"
> end
> end
>
> class A
> b =3D B.new
> end
>
> a =3D A.new
> a.b.fun_b
>
> The part that bugs me is this error:
> ./test.rb:15: undefined method `b' for #<A:0xb7cafb24> (NoMethodError)
>
> What is the way to code the above ideea, so that the last line would be c=

orrect.
>
> I'm running ruby 1.8.3 (2005-09-21) [i486-linux].
>


What problem do you want to solve?

Is this maybe nearer to you goals:

class A
class B
def fun_b
p "B"
end
end
end

A::B.new.fun_b

cheers,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      11-16-2005
Groleo Marius schrieb:
>
> class B
> def fun_b
> p "B"
> end
> end
>
> class A
> b = B.new
> end
>
> a = A.new
> a.b.fun_b
>
> The part that bugs me is this error:
> ./test.rb:15: undefined method `b' for #<A:0xb7cafb24> (NoMethodError)
>
> What is the way to code the above ideea, so that the last line would be correct.


Maybe this is what you want:

class A
attr_reader :b
def initialize
@b = B.new
end
end

Note that here each instance of A has its own instance of B, which might
not be what you want.

Regards,
Pit


 
Reply With Quote
 
Groleo Marius
Guest
Posts: n/a
 
      11-16-2005
On 11/16/05, Brian Schr=F6der <> wrote:
>
> What problem do you want to solve?
>
> Is this maybe nearer to you goals:
>
> class A
> class B
> def fun_b
> p "B"
> end
> end
> end
>
> A::B.new.fun_b
>
> cheers,
>
> Brian
>


Consider the case when class B would have more member functions.
In your example I would have to create a new object for each call to a
B function.
OTOH, a solution that I dont like could be :
class A
class B
def fun_b
p "B"
end
def other_b
p "_B"
end
end
end
b =3D A::B.new
b.fun_b
b.other_b

By the instantion of a new B object in the code from the first post,
and refering to it, I was searching to keep a hierarchy inside a
network protocol.
I couldn't find any hint on google though



--
Regards, Groleo!

# Use "Reply to All" on mailing lists.
# touch universe
# chmod +x universe
# ./universe


 
Reply With Quote
 
Brian Schröder
Guest
Posts: n/a
 
      11-16-2005
On 16/11/05, Groleo Marius <> wrote:
> On 11/16/05, Brian Schr=F6der <> wrote:
> >
> > What problem do you want to solve?
> >
> > Is this maybe nearer to you goals:
> >
> > class A
> > class B
> > def fun_b
> > p "B"
> > end
> > end
> > end
> >
> > A::B.new.fun_b
> >
> > cheers,
> >
> > Brian
> >

>
> Consider the case when class B would have more member functions.
> In your example I would have to create a new object for each call to a
> B function.
> OTOH, a solution that I dont like could be :
> class A
> class B
> def fun_b
> p "B"
> end
> def other_b
> p "_B"
> end
> end
> end
> b =3D A::B.new
> b.fun_b
> b.other_b
>
> By the instantion of a new B object in the code from the first post,
> and refering to it, I was searching to keep a hierarchy inside a
> network protocol.
> I couldn't find any hint on google though
>


Maybe you should look at the delegation pattern. That may be
applicable here. But I have not yet understood your use case. Could
you describe it in a bit more detail?

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


 
Reply With Quote
 
Groleo Marius
Guest
Posts: n/a
 
      11-16-2005
On 11/16/05, Brian Schr=F6der <> wrote:
> On 16/11/05, Groleo Marius <> wrote:
> > On 11/16/05, Brian Schr=F6der <> wrote:
> > >
> > > What problem do you want to solve?
> > >
> > > Is this maybe nearer to you goals:
> > >
> > > class A
> > > class B
> > > def fun_b
> > > p "B"
> > > end
> > > end
> > > end
> > >
> > > A::B.new.fun_b
> > >
> > > cheers,
> > >
> > > Brian
> > >

> >
> > Consider the case when class B would have more member functions.
> > In your example I would have to create a new object for each call to a
> > B function.
> > OTOH, a solution that I dont like could be :
> > class A
> > class B
> > def fun_b
> > p "B"
> > end
> > def other_b
> > p "_B"
> > end
> > end
> > end
> > b =3D A::B.new
> > b.fun_b
> > b.other_b
> >
> > By the instantion of a new B object in the code from the first post,
> > and refering to it, I was searching to keep a hierarchy inside a
> > network protocol.
> > I couldn't find any hint on google though
> >

>
> Maybe you should look at the delegation pattern. That may be
> applicable here. But I have not yet understood your use case. Could
> you describe it in a bit more detail?


Sure.
Consider a client script that has to querry a server.
The server operate on users/domains of a running qmail.

So, I would have a class named Client, with the folowing members:
login( user, pass ) logins to the remote administration server
domain an instantion of a domain class.

OTOH Domain class has X member functions:
add( domain_name) adds a new domain
remove
update

So a scenario would look like:

c =3DClient.new #creates a new Domain object
c.login( "foo", "bar" )
c.domain.add( "localdomain" )

Now , back to the code ,the attr_reader was the magic word from what
Pit posted .
Maybe you too observed, but why in ruby
this attr_reader : b is different from attr_reader :b
^space ^no space



--
Regards, Groleo!

# Use "Reply to All" on mailing lists.
# touch universe
# chmod +x universe
# ./universe


 
Reply With Quote
 
Brian Schröder
Guest
Posts: n/a
 
      11-16-2005
On 16/11/05, Groleo Marius <> wrote:
> On 11/16/05, Brian Schr=F6der <> wrote:
> > On 16/11/05, Groleo Marius <> wrote:
> > > On 11/16/05, Brian Schr=F6der <> wrote:
> > > >
> > > > What problem do you want to solve?
> > > >
> > > > Is this maybe nearer to you goals:
> > > >
> > > > class A
> > > > class B
> > > > def fun_b
> > > > p "B"
> > > > end
> > > > end
> > > > end
> > > >
> > > > A::B.new.fun_b
> > > >
> > > > cheers,
> > > >
> > > > Brian
> > > >
> > >
> > > Consider the case when class B would have more member functions.
> > > In your example I would have to create a new object for each call to =

a
> > > B function.
> > > OTOH, a solution that I dont like could be :
> > > class A
> > > class B
> > > def fun_b
> > > p "B"
> > > end
> > > def other_b
> > > p "_B"
> > > end
> > > end
> > > end
> > > b =3D A::B.new
> > > b.fun_b
> > > b.other_b
> > >
> > > By the instantion of a new B object in the code from the first post,
> > > and refering to it, I was searching to keep a hierarchy inside a
> > > network protocol.
> > > I couldn't find any hint on google though
> > >

> >
> > Maybe you should look at the delegation pattern. That may be
> > applicable here. But I have not yet understood your use case. Could
> > you describe it in a bit more detail?

>
> Sure.
> Consider a client script that has to querry a server.
> The server operate on users/domains of a running qmail.
>
> So, I would have a class named Client, with the folowing members:
> login( user, pass ) logins to the remote administration server
> domain an instantion of a domain class.
>
> OTOH Domain class has X member functions:
> add( domain_name) adds a new domain
> remove
> update
>
> So a scenario would look like:
>
> c =3DClient.new #creates a new Domain object
> c.login( "foo", "bar" )
> c.domain.add( "localdomain" )
>
> Now , back to the code ,the attr_reader was the magic word from what
> Pit posted .
> Maybe you too observed, but why in ruby
> this attr_reader : b is different from attr_reader :b
> ^space ^no space
>


Because :b is the symbol :b while : b is a colon followed by a b which
is not allowed syntax here.

cheers,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


 
Reply With Quote
 
Lloyd Zusman
Guest
Posts: n/a
 
      11-16-2005
Groleo Marius <> writes:

> [ ... ]
>
> Now , back to the code ,the attr_reader was the magic word from what
> Pit posted .
> Maybe you too observed, but why in ruby
> this attr_reader : b is different from attr_reader :b
> ^space ^no space


Because colon-identifier represents a symbol, and attr_reader takes
symbols as arguments. The colon is not a separator. For example:]

attr_reader :a, :b, :c

Each of :a, :b, and :c are symbols.


--
Lloyd Zusman

God bless you.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested classes within other classes Tony Johansson C++ 2 12-14-2004 11:41 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM
Nested Classes vs Top-Level Classes kelvSYC Java 2 08-18-2004 07:09 AM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57