February 2, 2004 Intro to Ruby Slide #26
Prev Next

Modules

Modules define namespaces (like packages in Perl).
     module Foo
       x = 42
     end

     puts Foo.x  # prints 42

Just as in Perl, we can put a bunch of definitions in one file:
     module MyMath
     
       PI = 3.14159
     
       def MyMath.square(x)
         return x * x
       end
     end

and use them in another file:
     require "MyMath"
     puts MyMath.square(MyMath::PI)  # prints 9.869587728


Prev Copyright © 2004 Walter C. Mankowski Next