简体   繁体   中英

Understanding ruby modules

I am new to Ruby, using eclipse, DLTK ruby plugin and Ruby 2.0. I created an Ruby module file called AModule.rb which has the code:

module AModule
  aConstant = 7
end

This is called from a test ruby script in the same project:

require 'AModule'
puts AModule::aConstant

In the second line of the above code, i get the error in '<main>': undefined local variable or method 'aModule' for main:Object (NameError)

I followed my codeacademy tutorial, so I did not expect this to happen. What is the mistake that I am making here ?

PS: Actually, I wanted to name my module file **aM**odule.rb and not **AM**odule.rb . But, the DLTK plugin quietly makes the first alphabet uppercase. Could this be a bug ?

Your problem is that name of constant must start with UpperCaseLetter. Otherwise Ruby will think of it as of local variable. So what's wrong with it? Short answer: it's all about scope. Local variables are only visible in their lexical scope. Constant is a quite different thing. Constant can always be accessed via so called namespace-resolution operator ( :: ) .

Read more info about Ruby scoping here .

I used windows cmd instead of eclipse IDE. I'll show the steps I used to find the errors for the code and finally fix them. DLTK plugin is not at fault here. Module file and test script are in the same folder.

LESSON - Module name and constant name in modules MUST begin with capital letter. Why, I don't know.

aModule.rb

module aModule
aConstant = 7
end

Test.rb

require 'aModule'
puts aModule::aConstant

cmd: cd into Test.rb folder and ruby Test.rb error: ``require': cannot load such file -- tokenizer.rb (LoadError)` help: Ruby 'require' error: cannot load such file

Ruby 1.9 has removed the current directory from the load path, and so you will need to do a relative require on this file, as Pascal says:

 require './tokenizer' 

There's no need to suffix it with .rb, as Ruby's smart enough to know that's what you mean anyway.

I made the following changes:

Test.rb

require './aModule'
puts aModule::aConstant

cmd: ruby Test.rb error: class/module name must be CONSTANT (SyntaxError) in aModule.rb help: NameError in Ruby

I made the following changes:

aModule.rb

module AModule # capital
aConstant = 7
end

Test.rb

require './AModule'
puts AModule::aConstant

cmd: ruby Test.rb error: undefined method 'aConstant' for AModule:Module (NoMethodError) help: Used the above link. I think constant must also be named with capital.

Final working code:

aModule.rb

module AModule # capital
AConstant = 7 # capital
end

Test.rb

require './AModule'
puts AModule::AConstant

WTF is Ruby like this ??? And why the hell do I need to append ./ before a module name when it is in the same folder as the test script ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM