I want to know, how to use modules in Ruby.
Given is a simple module
mymodule.rb
then I have this caller program
call_mymodule.rb
So far it worked ok:
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
I get an error
i.e. the constants work but the method's do not work.
I don't understand why. Do I have each time to use
or is there a better way ?
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]
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]))
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 !'
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]))
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 don't understand why. Do I have each time to use
Code:
require 'mymodule'
include Mymodule