[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Including Module

Thangappan Mohana sundaram

5/11/2009 8:43:00 AM


I am new to Ruby.
Is there any way to include the module in normal ruby file?

For an example,

Test.rb

Class Test.rb
........
end

testing.rb

object = Test.new # It tells error.
--
Posted via http://www.ruby-....

3 Answers

Ben Lovell

5/11/2009 9:09:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Mon, May 11, 2009 at 9:42 AM, Thangappan Mohana sundaram <
thangappanmohan@gmail.com> wrote:

>
> I am new to Ruby.
> Is there any way to include the module in normal ruby file?
>
> For an example,
>
> Test.rb
>
> Class Test.rb
> ........
> end
>
> testing.rb
>
> object = Test.new # It tells error.
>

In your testing.rb add a require like so:

require "Test"

at the top of the file.

Ben

Brian Candler

5/11/2009 9:10:00 AM

0

Thangappan Mohana sundaram wrote:
>
> I am new to Ruby.
> Is there any way to include the module in normal ruby file?
>
> For an example,
>
> Test.rb
>
> Class Test.rb
> ........
> end
>
> testing.rb

require 'Test'

> object = Test.new # It tells error.

Note that Test.rb should say 'class' not 'Class'

Also, it doesn't really matter what filename you use, but there is a
common convention that the name of the source file is the lowercased,
underscored version of the class name. So conventionally you would call
your file 'test.rb'. For a file containing class MyTestClass you would
call it my_test_class.rb

HTH,

Brian.
--
Posted via http://www.ruby-....

Stefano Crocco

5/11/2009 9:11:00 AM

0

On Monday 11 May 2009, Thangappan Mohana sundaram wrote:
> |I am new to Ruby.
> |Is there any way to include the module in normal ruby file?
> |
> |For an example,
> |
> |Test.rb
> |
> |Class Test.rb
> | ........
> |end
> |
> |testing.rb
> |
> |object = Test.new # It tells error.

Add:

require 'Test'

to testing.rb before calling Test.new (assuming that Test.rb and testing.rb
are in the same directory and that you run ruby from that directory). Also,
notice that:
1) you should write class Test, not class Test.rb (note the downcase c in
class and the absence of the .rb from Test). Class names have nothing in
common with file names
2) file names are (I think) case senstive in ruby, so if you call your file
Test.rb you'll need to require 'Test'; if you call it test.rb you'll need to
require 'test'. If you're on windows, which is not a case sensitive OS, you
may be used to a different behaviour (actually, I'm not completely sure of how
this behaves on windows, as I've never used ruby on it)
3) modules in ruby are something entirely different (see the documentation for
the Module class either at http://www.ruby-doc... or using ri:
ri Module.

For more information you can look at the documentation for the require method
of module Kernel, either at http://www.ruby-doc... or using ri: ri
Kernel#require. You may also find useful reading the free online version of
the Pickaxe book (http://www.ruby-doc.org/docs/Progra...) it's a bit
outdated, but it should be useful all the same.

I hope this helps

Stefano