Ruby unable to use require

path, require, ruby

Solution

While the top-level of all files are executed in the same context, each file has its own script context for local variables. In other words, each file has its own set of local variables that can be accessed throughout that file, but not in other files.

On the other hand, constants (CodeWords), globals ($code_words) and methods (def code_words) would be accessible across files.

Some solutions:

CodeWords = {:real => "code"}

$code_words = {:real => "code"}

def code_words
  {:real => "code"}
end

An OO solution that is definitely too complex for this case:

# first file
class CodeWords
  DEFAULT = {:real => "code"}

  attr_reader :words
  def initialize(words = nil)
    @words = words || DEFAULT
  end
end

# second file
print "Enter your ideas "
idea = gets
code_words = CodeWords.new
code_words.words.each do |real, code|
  idea.gsub!(real, code)
end

#Save the gibberish to a new file
print "File encoded, please enter a name to save the file"
ideas_name = gets.strip
File::open( 'idea-' + ideas_name + '.txt', 'w' ) do |f|
  f << idea
end

Problem

This is a newbie question as I am attempting to learn Ruby by myself, so apologies if it sounds like a silly question! I am reading through the examples of why's (poignant) guide to ruby and am in chapter 4. I typed the code_words Hash into a file called wordlist.rb I opened another file and typed the first line as require 'wordlist.rb' and the rest of the code as below ``` #Get evil idea and swap in code print "Enter your ideas " idea = gets code_words.each do |real, code| idea.gsub!(real, code) end #Save the gibberish to a new file print "File encoded, please enter a name to save the file" ideas_name = gets.strip File::open( 'idea-' + ideas_name + '.txt', 'w' ) do |f| f << idea end ``` When I execute this code, it fails with the following error message: C:/MyCode/MyRubyCode/filecoder.rb:5: undefined local variable or method `code_words' for main:Object (NameError) I use Windows XP and Ruby version ruby 1.8.6 I know I should be setting something like a ClassPath, but not sure where/how to do so! Many thanks in advance!

Original source

Related problems