Forum: Ruby Nested classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C41701b0b2a2b8e92acf4f87e9cdd808?d=identicon&s=25 groleo (Guest)
on 2005-11-16 10:08
(Received via mailing list)
hi list.

I have the following code:

#! /usr/bin/ruby


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.

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
6b4566518f6675477dab9b8ba813cf3c?d=identicon&s=25 ruby.brian (Guest)
on 2005-11-16 10:14
(Received via mailing list)
On 16/11/05, Groleo Marius <groleo@gmail.com> wrote:
>     end
> ./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.
>
> 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/
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 pit (Guest)
on 2005-11-16 10:26
(Received via mailing list)
Groleo Marius schrieb:
>
> 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
C41701b0b2a2b8e92acf4f87e9cdd808?d=identicon&s=25 groleo (Guest)
on 2005-11-16 10:35
(Received via mailing list)
On 11/16/05, Brian Schröder <ruby.brian@gmail.com> wrote:
>   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 = 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
6b4566518f6675477dab9b8ba813cf3c?d=identicon&s=25 ruby.brian (Guest)
on 2005-11-16 10:44
(Received via mailing list)
On 16/11/05, Groleo Marius <groleo@gmail.com> wrote:
> >     end
> Consider the case when class B would have more member functions.
>      end
>
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/
C41701b0b2a2b8e92acf4f87e9cdd808?d=identicon&s=25 groleo (Guest)
on 2005-11-16 11:02
(Received via mailing list)
On 11/16/05, Brian Schröder <ruby.brian@gmail.com> wrote:
> > >       p "B"
> >
> >        p "_B"
> > 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 =Client.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
6b4566518f6675477dab9b8ba813cf3c?d=identicon&s=25 ruby.brian (Guest)
on 2005-11-16 11:20
(Received via mailing list)
On 16/11/05, Groleo Marius <groleo@gmail.com> wrote:
> > > >     def fun_b
> > > >
> > >      def other_b
> > > network protocol.
>
>
> c =Client.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/
56f2ce19706d05d18b5b66483aa13f98?d=identicon&s=25 ljz (Guest)
on 2005-11-16 11:35
(Received via mailing list)
Groleo Marius <groleo@gmail.com> 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.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-10-09 20:01
(Received via mailing list)
On Oct 8, 2013, at 9:40 AM, Xeno Campanoli <xeno.campanoli@gmail.com>
wrote:

>> Is it good idea to use nested classes if they are small and will be used by
parent class only?
>>
>> Bob
>>
>

You can check out any time you like, but you can never leave.
This topic is locked and can not be replied to.