Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Nested Modules/Classes

Reply
Thread Tools

Nested Modules/Classes

 
 
Ken Mitchell
Guest
Posts: n/a
 
      04-02-2007
I have what I feel should be a simple issue. Look at the following:

############################
# Test of nested modules/classes
############################
class Global
module TestMod
TEST = "test1"
def print_test
puts "test2"
end
end#module TestMod

include TestMod
puts TEST
print_test

end#class Global
##############################

->test1
->test.rb:22: undefined local variable or method 'print_test' for
Global:Class

Whether "Global" is a Class or a Method the same happens. However, if I
comment out "Global" completely, out put is as expected:

->test1
->test2

Thanks in advance.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ryan Leavengood
Guest
Posts: n/a
 
      04-02-2007
On 4/2/07, Ken Mitchell <(E-Mail Removed)> wrote:
> I have what I feel should be a simple issue. Look at the following:
>
> ############################
> # Test of nested modules/classes
> ############################
> class Global
> module TestMod
> TEST = "test1"
> def print_test
> puts "test2"
> end
> end#module TestMod
>
> include TestMod


You only include the module, therefore any methods defined in it will
only be instance methods. By trying to call print_test from inside the
class definition you are calling it as a class method. If you want
print_test to also be a class method you must extend TestMod as well
as including it.

Basically include adds the module's methods as instance methods and
extend adds them as class methods.

Ryan

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      04-02-2007
On Tue, Apr 03, 2007 at 06:02:41AM +0900, Ken Mitchell wrote:
> I have what I feel should be a simple issue. Look at the following:
>
> ############################
> # Test of nested modules/classes
> ############################
> class Global
> module TestMod
> TEST = "test1"
> def print_test
> puts "test2"
> end
> end#module TestMod
>
> include TestMod
> puts TEST
> print_test
>
> end#class Global
> ##############################
>
> ->test1
> ->test.rb:22: undefined local variable or method 'print_test' for
> Global:Class
>
> Whether "Global" is a Class or a Method the same happens. However, if I
> comment out "Global" completely, out put is as expected:
>
> ->test1
> ->test2


If you 'include' a module in a class, its methods are added as instance
methods for objects of that class. Try:

a = Global.new
a.print_test

 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      04-02-2007
Alle luned=C3=AC 2 aprile 2007, Ken Mitchell ha scritto:
> I have what I feel should be a simple issue. Look at the following:
>
> ############################
> # Test of nested modules/classes
> ############################
> class Global
> module TestMod
> TEST =3D "test1"
> def print_test
> puts "test2"
> end
> end#module TestMod
>
> include TestMod
> puts TEST
> print_test
>
> end#class Global
> ##############################
>
> ->test1
> ->test.rb:22: undefined local variable or method 'print_test' for
> Global:Class
>
> Whether "Global" is a Class or a Method the same happens. However, if I
> comment out "Global" completely, out put is as expected:
>
> ->test1
> ->test2
>
> Thanks in advance.


When you include TestMod in the class Global, the instance methods of TestM=
od=20
become instance methods of Global, that is, they become methods of the=20
instances of Global, not of Global itself. So, you can do:

Global.new.print_test
=3D> "test2"

The same happens if Global were a module.

If you want to add the instance methods of TestMod to Global itself, instea=
d,=20
you can do the following:

class Global
module TestMod
...
end #End of TestMod
extend TestMod
end

While Module#include adds the instance methods of the module as instance=20
methods of the class (or module) where it's called, extend adds the instanc=
e=20
methods of the module to its receiver (self, in this case).

I hope this helps

Stefano

 
Reply With Quote
 
Ken Mitchell
Guest
Posts: n/a
 
      04-02-2007
Brian Candler wrote:
> On Tue, Apr 03, 2007 at 06:02:41AM +0900, Ken Mitchell wrote:
>> end
>> ->test.rb:22: undefined local variable or method 'print_test' for
>> Global:Class
>>
>> Whether "Global" is a Class or a Method the same happens. However, if I
>> comment out "Global" completely, out put is as expected:
>>
>> ->test1
>> ->test2

>
> If you 'include' a module in a class, its methods are added as instance
> methods for objects of that class. Try:
>
> a = Global.new
> a.print_test


Thank you two! See, I knew it would be simple and now it even makes
sense.

So in the context of the above, my application will be performing a wide
range of work and will need to be quite modular (ok I'm lazy). As far
as preference and "best practice", how would I best layout the
namespace. For instance:

#/lib/testapp.rb
#/lib/testapp/config.rb
#/lib/testapp/logger.rb

maybe this....

module Testapp
module config
end

class logger
end
end

Looking through included source, I see this done so many different ways.
What is the best. Thanks so much

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ken Mitchell
Guest
Posts: n/a
 
      04-02-2007
Sorry for the Double Post, thank you Stefano I missed you.

Where is the mailing list located. I cannot find it, only these forums.
It seems that some of you guys are using maillist functionality


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ilan Berci
Guest
Posts: n/a
 
      04-02-2007
Ken Mitchell wrote:
> I have what I feel should be a simple issue. Look at the following:
>
> ############################
> # Test of nested modules/classes
> ############################
> class Global
> module TestMod
> TEST = "test1"
> def print_test
> puts "test2"
> end
> end#module TestMod
>
> include TestMod
> puts TEST
> print_test
>
> end#class Global
> ##############################
>
> ->test1
> ->test.rb:22: undefined local variable or method 'print_test' for
> Global:Class
>
> Whether "Global" is a Class or a Method the same happens. However, if I
> comment out "Global" completely, out put is as expected:
>
> ->test1
> ->test2
>
> Thanks in advance.



irb(main):004:0> class Global
irb(main):005:1> module TestMod
irb(main):006:2> TEST = "test1"
irb(main):007:2> def print_test
irb(main):008:3> puts "test2"
irb(main):009:3> end
irb(main):010:2> end
irb(main):011:1> extend TestMod
irb(main):012:1> end
=> Global
irb(main):013:0> Global.print_test
test2
=> nil

ilan


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ryan Leavengood
Guest
Posts: n/a
 
      04-02-2007
On 4/2/07, Ken Mitchell <(E-Mail Removed)> wrote:
>
> Where is the mailing list located. I cannot find it, only these forums.
> It seems that some of you guys are using maillist functionality


Hi Ken,

The Ruby Forum is useful for the occasional post. It forwards your
messages to the ruby-talk mailing list. If you would like to subscribe
to ruby-talk go here:

http://www.ruby-lang.org/en/community/mailing-lists/

You can learn a lot about Ruby by reading the messages posted here.

Regarding your question about how to structure your Ruby code, here
are my suggestions:

- create a Module as the namespace for all the classes in your
application or library.
- create the classes you need inside that module.
- if you find classes with common functionality, see if you can break
that out into modules which are included in those classes.
- I recommend using the Test::Unit library to unit test your classes.
An example of this is in my recent Ruby Quiz submission:

http://blade.nagaokaut.ac.jp/cgi-bin...by-talk/246269

Good luck with Ruby and welcome to our community. As you can see we
are a pretty friendly and helpful bunch

Regards,
Ryan

 
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 friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 PM
dealing with nested xml within nested xml within...... Ultrus Python 3 07-09-2007 09:00 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Vector Nester Classes are Nested in my Brain Chad E. Dollins C++ 3 11-08-2005 04:46 AM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM



Advertisments