Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ruby modules - require and incude 1

Status
Not open for further replies.

mikrom

Programmer
Mar 27, 2002
2,985
SK
I want to know, how to use modules in Ruby.
Given is a simple module
mymodule.rb
Code:
[COLOR=#a020f0]module[/color] [COLOR=#2e8b57][b]Mymodule[/b][/color]
  [COLOR=#2e8b57][b]SQRT2[/b][/color] = [COLOR=#ff00ff]1.41[/color]
  [COLOR=#2e8b57][b]E[/b][/color] = [COLOR=#ff00ff]2.17[/color]
  [COLOR=#2e8b57][b]PI[/b][/color] = [COLOR=#ff00ff]3.14[/color]
  [COLOR=#a020f0]def[/color] [COLOR=#008080]mysqrt[/color](x)
    [COLOR=#804040][b]return[/b][/color] [COLOR=#2e8b57][b]Math[/b][/color].sqrt(x)
  [COLOR=#a020f0]end[/color]
  [COLOR=#a020f0]def[/color] [COLOR=#008080]myhallo[/color](x)
    [COLOR=#804040][b]return[/b][/color] [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Hallo [/color][COLOR=#6a5acd]#{[/color]x[COLOR=#6a5acd]}[/color][COLOR=#ff00ff] ![/color][COLOR=#6a5acd]"[/color]
  [COLOR=#a020f0]end[/color]
[COLOR=#a020f0]end[/color]

puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]__FILE__ = '[/color][COLOR=#6a5acd]#{[/color][COLOR=#ff00ff]__FILE__[/color][COLOR=#6a5acd]}[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]$0 = '[/color][COLOR=#6a5acd]#{[/color][COLOR=#008080]$0[/color][COLOR=#6a5acd]}[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]"[/color]
puts 
[COLOR=#0000ff]# __FILE__ is the name of the file.[/color]
[COLOR=#0000ff]# $0 is the name of the program that was called.[/color]
[COLOR=#0000ff]# When they match, the statement is evaluated as true[/color]

[COLOR=#804040][b]if[/b][/color] [COLOR=#ff00ff]__FILE__[/color] == [COLOR=#008080]$0[/color]
  [COLOR=#0000ff]# test this module[/color]
  [COLOR=#a020f0]include[/color] [COLOR=#2e8b57][b]Mymodule[/b][/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]*** Now testing module 'Mymodule' ***[/color][COLOR=#6a5acd]"[/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]SQRT2 = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]SQRT2[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]E = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]E[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]PI = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]PI[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
  printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]mysqrt(%f) = %f[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#ff00ff]2[/color], mysqrt([COLOR=#ff00ff]2[/color]))
  printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]myhallo('%s') = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color], myhallo([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color]))
[COLOR=#804040][b]end[/b][/color]
then I have this caller program
call_mymodule.rb
Code:
[COLOR=#a020f0]require[/color] [COLOR=#6a5acd]'[/color][COLOR=#ff00ff]mymodule[/color][COLOR=#6a5acd]'[/color]
[COLOR=#a020f0]include[/color] [COLOR=#2e8b57][b]Mymodule[/b][/color] [COLOR=#0000ff]# using the namespace Mymodule[/color]

puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]*** Using module 'Mymodule' ***[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]SQRT2 = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]SQRT2[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]E = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]E[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]PI = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]PI[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]mysqrt(%f) = %f[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#ff00ff]2[/color], mysqrt([COLOR=#ff00ff]2[/color]))
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]myhallo('%s') = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color], myhallo([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color]))
So far it worked ok:
Code:
C:\Users\Roman\Work\Ruby\Modules>ruby call_mymodule.rb
__FILE__ = './mymodule.rb'
$0 = 'call_mymodule.rb'

*** Using module 'Mymodule' ***
SQRT2 = 1.41
E = 2.17
PI = 3.14
mysqrt(2.000000) = 1.414214
myhallo('Roman') = 'Hallo Roman !'
But when I try to use only require without include, then the module constants work, but the methods don't work, e.g. if I try this
call_mymodule2.rb
Code:
[COLOR=#a020f0]require[/color] [COLOR=#6a5acd]'[/color][COLOR=#ff00ff]mymodule[/color][COLOR=#6a5acd]'[/color]
[COLOR=#0000ff]#include Mymodule # using the namespace Mymodule[/color]

puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]*** Using module 'Mymodule' ***[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]SQRT2 = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Mymodule[/b][/color]::[COLOR=#2e8b57][b]SQRT2[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]E = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Mymodule[/b][/color]::[COLOR=#2e8b57][b]E[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]PI = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Mymodule[/b][/color]::[COLOR=#2e8b57][b]PI[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]mysqrt(%f) = %f[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#ff00ff]2[/color], [COLOR=#2e8b57][b]Mymodule[/b][/color]::mysqrt([COLOR=#ff00ff]2[/color]))
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]myhallo('%s') = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color], [COLOR=#2e8b57][b]Mymodule[/b][/color]::myhallo([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color]))
I get an error
Code:
C:\Users\Roman\Work\Ruby\Modules>ruby call_mymodule2.rb
__FILE__ = './mymodule.rb'
$0 = 'call_mymodule2.rb'

*** Using module 'Mymodule' ***
SQRT2 = 1.41
E = 2.17
PI = 3.14
[COLOR=red]call_mymodule2.rb:8: undefined method `mysqrt' for Mymodule:Module (NoMethodError)[/color]
i.e. the constants work but the method's do not work.
I don't understand why. Do I have each time to use
Code:
require 'mymodule'
include Mymodule
or is there a better way ?
 
I redefined
mymodule.rb
Code:
[COLOR=#a020f0]module[/color] [COLOR=#2e8b57][b]Mymodule[/b][/color]
  [COLOR=#2e8b57][b]SQRT2[/b][/color] = [COLOR=#ff00ff]1.41[/color]
  [COLOR=#2e8b57][b]E[/b][/color] = [COLOR=#ff00ff]2.17[/color]
  [COLOR=#2e8b57][b]PI[/b][/color] = [COLOR=#ff00ff]3.14[/color]
  [COLOR=#a020f0]def[/color] [COLOR=#2e8b57][b]Mymodule[/b][/color].[COLOR=#008080]mysqrt[/color](x)
    [COLOR=#804040][b]return[/b][/color] [COLOR=#2e8b57][b]Math[/b][/color].sqrt(x)
  [COLOR=#a020f0]end[/color]
  [COLOR=#a020f0]def[/color] [COLOR=#2e8b57][b]Mymodule[/b][/color].[COLOR=#008080]myhallo[/color](x)
    [COLOR=#804040][b]return[/b][/color] [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Hallo [/color][COLOR=#6a5acd]#{[/color]x[COLOR=#6a5acd]}[/color][COLOR=#ff00ff] ![/color][COLOR=#6a5acd]"[/color]
  [COLOR=#a020f0]end[/color]
[COLOR=#a020f0]end[/color]

puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]__FILE__ = '[/color][COLOR=#6a5acd]#{[/color][COLOR=#ff00ff]__FILE__[/color][COLOR=#6a5acd]}[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]$0 = '[/color][COLOR=#6a5acd]#{[/color][COLOR=#008080]$0[/color][COLOR=#6a5acd]}[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]"[/color]
puts 
[COLOR=#0000ff]# __FILE__ is the name of the file.[/color]
[COLOR=#0000ff]# $0 is the name of the program that was called.[/color]
[COLOR=#0000ff]# When they match, the statement is evaluated as true[/color]

[COLOR=#804040][b]if[/b][/color] [COLOR=#ff00ff]__FILE__[/color] == [COLOR=#008080]$0[/color]
  [COLOR=#0000ff]# test this module[/color]
  [COLOR=#a020f0]include[/color] [COLOR=#2e8b57][b]Mymodule[/b][/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]*** Now testing module 'Mymodule' ***[/color][COLOR=#6a5acd]"[/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]SQRT2 = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]SQRT2[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]E = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]E[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]PI = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]PI[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
  printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]mysqrt(%f) = %f[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#ff00ff]2[/color], [COLOR=#2e8b57][b]Mymodule[/b][/color].mysqrt([COLOR=#ff00ff]2[/color]))
  printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]myhallo('%s') = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color], [COLOR=#2e8b57][b]Mymodule[/b][/color].myhallo([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color]))
[COLOR=#804040][b]end[/b][/color]
and now
call_mymodule.rb
Code:
[COLOR=#a020f0]require[/color] [COLOR=#6a5acd]'[/color][COLOR=#ff00ff]mymodule[/color][COLOR=#6a5acd]'[/color]
[COLOR=#a020f0]include[/color] [COLOR=#2e8b57][b]Mymodule[/b][/color] [COLOR=#0000ff]# using the namespace Mymodule[/color]

puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]*** Using module 'Mymodule' ***[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]SQRT2 = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]SQRT2[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]E = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]E[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]PI = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]PI[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]mysqrt(%f) = %f[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#ff00ff]2[/color], [COLOR=#2e8b57][b]Mymodule[/b][/color].mysqrt([COLOR=#ff00ff]2[/color]))
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]myhallo('%s') = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color], [COLOR=#2e8b57][b]Mymodule[/b][/color].myhallo([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color]))
and
call_mymodule2.rb
Code:
[COLOR=#a020f0]require[/color] [COLOR=#6a5acd]'[/color][COLOR=#ff00ff]mymodule[/color][COLOR=#6a5acd]'[/color]
[COLOR=#0000ff]#include Mymodule # using the namespace Mymodule[/color]

puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]*** Using module 'Mymodule' ***[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]SQRT2 = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Mymodule[/b][/color]::[COLOR=#2e8b57][b]SQRT2[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]E = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Mymodule[/b][/color]::[COLOR=#2e8b57][b]E[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]PI = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Mymodule[/b][/color]::[COLOR=#2e8b57][b]PI[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]mysqrt(%f) = %f[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#ff00ff]2[/color], [COLOR=#2e8b57][b]Mymodule[/b][/color].mysqrt([COLOR=#ff00ff]2[/color]))
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]myhallo('%s') = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color], [COLOR=#2e8b57][b]Mymodule[/b][/color].myhallo([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Roman[/color][COLOR=#6a5acd]"[/color]))
work:
Code:
C:\Users\Roman\Work\Ruby\Modules>ruby call_mymodule.rb
__FILE__ = './mymodule.rb'
$0 = 'call_mymodule.rb'

*** Using module 'Mymodule' ***
SQRT2 = 1.41
E = 2.17
PI = 3.14
mysqrt(2.000000) = 1.414214
myhallo('Roman') = 'Hallo Roman !'

C:\Users\Roman\Work\Ruby\Modules>ruby call_mymodule2.rb
__FILE__ = './mymodule.rb'
$0 = 'call_mymodule2.rb'

*** Using module 'Mymodule' ***
SQRT2 = 1.41
E = 2.17
PI = 3.14
mysqrt(2.000000) = 1.414214
myhallo('Roman') = 'Hallo Roman !'

Can somebody say me, how to use the modules in Ruby?
 
Hi

[tt]require[/tt] is to include code from external file.

[tt]include[/tt] is to import things defined in another namespace.

That word, namespace, was the one which actually explained to me what modules are : just logical grouping of the methods and constants. Roughly speaking.

Programming Ruby | Modules explains it better.


Feherke.
 
Hi feherke,

First, thank you very much for that link.

The Ruby modules seems to be a little bit complicated.
I thought If I use include I don't need to use the module prefix in constants and methods - but it's not so simple.

1. The module constants and the module methods are treated very differently:
In my second example if I use
Code:
include Mymodule
I can use the constant PI without prefix (Mymodule::) but the module method (i.e. the prefixed method) myhallo doesn't work without prefix, so I need to use Mymodule.myhallo.
The prefixed methods need to be used every time with prefix.

2. Without using
Code:
include Mymodule
there is no way for using unprefixed methods - e.g. hallo from my first example.
Are the unprefixed methods primarily used for mixins only?
 
Here I summarize on an example, how do I create and use a module in Ruby:

trymodule.rb
Code:
[COLOR=#a020f0]module[/color] [COLOR=#2e8b57][b]Trymodule[/b][/color]
  [COLOR=#2e8b57][b]E[/b][/color] = [COLOR=#ff00ff]2.71[/color]            [COLOR=#0000ff]# module constant[/color]
  [COLOR=#a020f0]def[/color] [COLOR=#2e8b57][b]Trymodule[/b][/color].[COLOR=#008080]hi[/color](s) [COLOR=#0000ff]# prefixed method - aka module method[/color]
    [COLOR=#804040][b]return[/b][/color] [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Hi [/color][COLOR=#6a5acd]#{[/color]s[COLOR=#6a5acd]}[/color][COLOR=#ff00ff] ![/color][COLOR=#6a5acd]"[/color]
  [COLOR=#a020f0]end[/color]
  [COLOR=#a020f0]def[/color] [COLOR=#008080]cheers[/color](s)       [COLOR=#0000ff]# unprefixed method[/color]
    [COLOR=#804040][b]return[/b][/color] [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Cheers [/color][COLOR=#6a5acd]#{[/color]s[COLOR=#6a5acd]}[/color][COLOR=#ff00ff] ![/color][COLOR=#6a5acd]"[/color]
  [COLOR=#a020f0]end[/color]
  [COLOR=#0000ff]#[/color]
  [COLOR=#a020f0]class[/color] [COLOR=#2e8b57][b]Myclass[/b][/color]
    [COLOR=#2e8b57][b]PI[/b][/color] = [COLOR=#ff00ff]3.14[/color]                [COLOR=#0000ff]# class constant[/color]
    [COLOR=#a020f0]def[/color] [COLOR=#2e8b57][b]Myclass[/b][/color].[COLOR=#008080]greet[/color](s)     [COLOR=#0000ff]# class method[/color]
      [COLOR=#804040][b]return[/b][/color] [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Greetings [/color][COLOR=#6a5acd]#{[/color]s[COLOR=#6a5acd]}[/color][COLOR=#ff00ff] ![/color][COLOR=#6a5acd]"[/color]
    [COLOR=#a020f0]end[/color]
    [COLOR=#a020f0]def[/color] [COLOR=#008080]hello[/color](s)             [COLOR=#0000ff]# instance method[/color]
      [COLOR=#804040][b]return[/b][/color] [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Hello [/color][COLOR=#6a5acd]#{[/color]s[COLOR=#6a5acd]}[/color][COLOR=#ff00ff] ![/color][COLOR=#6a5acd]"[/color]
    [COLOR=#a020f0]end[/color]
  [COLOR=#a020f0]end[/color]
[COLOR=#a020f0]end[/color]

puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]current file   = '[/color][COLOR=#6a5acd]#{[/color][COLOR=#ff00ff]__FILE__[/color][COLOR=#6a5acd]}[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]called program = '[/color][COLOR=#6a5acd]#{[/color][COLOR=#008080]$0[/color][COLOR=#6a5acd]}[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]"[/color]
puts
[COLOR=#0000ff]# __FILE__ is the name of the file.[/color]
[COLOR=#0000ff]# $0 is the name of the program that was called.[/color]
[COLOR=#804040][b]if[/b][/color] [COLOR=#ff00ff]__FILE__[/color] == [COLOR=#008080]$0[/color]
  [COLOR=#0000ff]# test this module[/color]
  [COLOR=#a020f0]include[/color] [COLOR=#2e8b57][b]Trymodule[/b][/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]*** Now testing module 'Trymodule' ***[/color][COLOR=#6a5acd]"[/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]E  = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Trymodule[/b][/color]::[COLOR=#2e8b57][b]E[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color] [COLOR=#0000ff]# print module constant[/color]
  puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]PI = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Myclass[/b][/color]::[COLOR=#2e8b57][b]PI[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]  [COLOR=#0000ff]# print class constant[/color]
  name = [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]mikrom[/color][COLOR=#6a5acd]"[/color]
  [COLOR=#0000ff]# call prefixed method (aka module method)[/color]
  printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Trymodule.hi('%s')  = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], name, [COLOR=#2e8b57][b]Trymodule[/b][/color].hi(name))  
  [COLOR=#0000ff]# call unprefixed method[/color]
  printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]cheers('%s')        = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], name, cheers(name))
  [COLOR=#0000ff]# call class method[/color]
  printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Myclass.greet('%s') = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], name, [COLOR=#2e8b57][b]Myclass[/b][/color].greet(name))  
  [COLOR=#0000ff]# call instance method[/color]
  myobject = [COLOR=#2e8b57][b]Myclass[/b][/color].new() [COLOR=#0000ff]# create instance[/color]
  printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]hello('%s')         = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], name, myobject.hello(name))
[COLOR=#804040][b]end[/b][/color]

trymodule_call.rb
Code:
[COLOR=#a020f0]require[/color] [COLOR=#6a5acd]'[/color][COLOR=#ff00ff]trymodule[/color][COLOR=#6a5acd]'[/color] [COLOR=#0000ff]# load file trymodule.rb[/color]
[COLOR=#a020f0]include[/color] [COLOR=#2e8b57][b]Trymodule[/b][/color]   [COLOR=#0000ff]# make reference to the module Trymodule[/color]

puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]*** Using module 'Trymodule' ***[/color][COLOR=#6a5acd]"[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]E  = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Trymodule[/b][/color]::[COLOR=#2e8b57][b]E[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color] [COLOR=#0000ff]# print module constant[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]PI = [/color][COLOR=#6a5acd]#{[/color][COLOR=#2e8b57][b]Myclass[/b][/color]::[COLOR=#2e8b57][b]PI[/b][/color][COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]  [COLOR=#0000ff]# print class constant[/color]
name = [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]feherke[/color][COLOR=#6a5acd]"[/color]
[COLOR=#0000ff]# call prefixed method (aka module method)[/color]
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Trymodule.hi('%s')  = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], name, [COLOR=#2e8b57][b]Trymodule[/b][/color].hi(name))  
[COLOR=#0000ff]# call unprefixed method[/color]
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]cheers('%s')        = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], name, cheers(name))
[COLOR=#0000ff]# call class method[/color]
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]Myclass.greet('%s') = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], name, [COLOR=#2e8b57][b]Myclass[/b][/color].greet(name))
[COLOR=#0000ff]# call instance method[/color]
myobject = [COLOR=#2e8b57][b]Myclass[/b][/color].new() [COLOR=#0000ff]# create instance[/color]
printf([COLOR=#6a5acd]"[/color][COLOR=#ff00ff]hello('%s')         = '%s'[/color][COLOR=#6a5acd]\n[/color][COLOR=#6a5acd]"[/color], name, myobject.hello(name))

In the file trymodule.rb I define a module Trymodule which contains a module constant, a module method (i.e. prefixed method), an unprefixed method and a class with a class constant, class method and an instance method. If I call the module source file directly, then it executes the module self-test:
Code:
D:\Work\Ruby\Modules>ruby trymodule.rb
current file   = 'trymodule.rb'
called program = 'trymodule.rb'

*** Now testing module 'Trymodule' ***
E  = 2.71
PI = 3.14
Trymodule.hi('mikrom')  = 'Hi mikrom !'
cheers('mikrom')        = 'Cheers mikrom !'
Myclass.greet('mikrom') = 'Greetings mikrom !'
hello('mikrom')         = 'Hello mikrom !'

In the file trymodule_call.rb I'm using the module, it outputs:
Code:
D:\Work\Ruby\Modules>ruby trymodule_call.rb
current file   = './trymodule.rb'
called program = 'trymodule_call.rb'

*** Using module 'Trymodule' ***
E  = 2.71
PI = 3.14
Trymodule.hi('feherke')  = 'Hi feherke !'
cheers('feherke')        = 'Cheers feherke !'
Myclass.greet('feherke') = 'Greetings feherke !'
hello('feherke')         = 'Hello feherke !'

Your opinion would be greatly appreciated.
 
Hi

mikrom said:
Are the unprefixed methods primarily used for mixins only?
As far as I know, yes.
mikrom said:
Your opinion would be greatly appreciated.
Not so sure. I use modules to separate the code. For example the array functions in PHP mostly have "array_" prefix in their name. But there are exceptions too. And when you are tired you wish that all array functions would have the same prefix. And this is where modules help.

I do not include them, so I have no proper opinion. Sorry. But if I think to the following :
Code:
[gray]# I prefer this[/gray]
[red]require[/red] [green][i]'sqlite3'[/i][/green]
db[teal]=[/teal]SQLite3[teal]::[/teal]Database[teal].[/teal]new database

[gray]# instead of this[/gray]
[red]require[/red] [green][i]'sqlite3'[/i][/green]
[b]include[/b] SQLite3
db[teal]=[/teal]Database[teal].[/teal]new database
Personally I would use [tt]include[/tt] only for mixins.


Feherke.
 
Thanks you very much for your opinions.
I used include in my above example, because without using it I couldn't use the unprefixed method, which I have in my module.
But as you confirmed my presumption, that unprefixed methods are used primarily for mixins, I don't need them (for simple purposes) and so I don't need to include the module.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top