Wednesday 21 August 2013

Ruby: How do I access module local variables?

Ruby: How do I access module local variables?

I get this error: MyModule.rb:4:in getName': undefined local variable or
methods' for MyModule:Module (NameError)
file1
module MyModule
s = "some name"
def self.getName()
puts s
end
end
file2
require './MyModule.rb'
include MyModule
MyModule.getName()
This has something to do with scope, but I'm not comprehending why this is
happening if I declared it before the method. does include only mixin
methods and not variables? How do I change my module so that it can print
out variables I define within the module?

No comments:

Post a Comment