I have the following code: http://scrp.at/FB
# Error:
# bin/rpg:5:in `<main>': uninitialized constant RubyPasswordGenerator::Korben (NameError)
In file “bin/rpg”
#!/usr/bin/env ruby
require_relative "../lib/ruby_password_generator"
puts RubyPasswordGenerator::Korben.new
In “lib/ruby_password_generator.rb”
require_relative "ruby_password_generator/ruby_password_generator"
require_relative "ruby_password_generator/password_generator"
require_relative "ruby_password_generator/version"
# DEBUG
require "pp"
module RubyPasswordGenerator
end
require_relative "ruby_password_generator/algo/korben"
In “algo/korben.rb”
module RubyPasswordGenrator
class Korben
M_LOWERCAS_LETTERS = ("a".."z").to_a
M_UPPERCASE_LETTERS = ("A".."Z").to_a
M_NUMBERS = (0..9).to_a
M_SPECCIAL_CHARACTERS = "!@#()_-+=[]{}".split("")
def initialize(length = 42)
raise ArgumentError unless length.is_a?(Integer)
raise ArgumentError unless length >= 3 && length <= 255
@length = length
end
def generate
password = ""
(0...@length).each do
char = (M_LOWERCAS_LETTERS + M_UPPERCASE_LETTERS + M_NUMBERS + M_SPECCIAL_CHARACTERS).shuffle.sample
password << char
end
password
end
end
end
The file structure looks like:
# .
# ├── Gemfile
# ├── Gemfile.lock
# ├── LICENSE.markdown
# ├── NERD_tree_3
# ├── README.markdown
# ├── Rakefile
# ├── bin
# │ └── rpg
# ├── lib
# │ ├── ruby_password_generator
# │ │ ├── algo
# │ │ │ ├── korben.rb
# │ │ │ └── marvin.rb
# │ │ ├── helpers
# │ │ ├── password_generator.rb
# │ │ ├── ruby_password_generator.rb
# │ │ └── version.rb
# │ └── ruby_password_generator.rb
# ├── ruby_password_generator.gemspec
# └── spec
# ├── algo
# │ ├── korben_spec.rb
# │ └── marvin_spec.rb
# ├── password_generator_spec.rb
# ├── ruby_password_generator_spec.rb
# └── spec_helper.rb
#
# 7 directories, 19 files
I really don’t know why I am getting an uninitialized constant “NameError” error. I included the file properly using require_relative. I also tried autoload and require, but that didn’t solve anything. I am using ruby 1.9.2-p180.
If anybody knows what’s going on please explain me.
You’re missing an e in the module name. Thus the Korben class exists in the
RubyPasswordGenratormodule not theRubyPasswordGeneratormodule andRubyPasswordGenerator::Korbendoes indeed not exist.