From: Justin Collins Date: 2010-01-18T19:00:22+09:00 Subject: Re: Newbie needs help getting started Cary Swoveland wrote: > I have just written the code for my first Ruby program, one that finds > the shortest path from start to finish in a maze. All i/o is through > files and a terminal. I have now turned to debugging it. To help me, I > have the books, "The Ruby Programming Language" and "Ruby Cookbook", and > of course vast internet resources. I'm using Ruby 1.8.7 on a MacBook. I > need a few answers to help me get started. > > 1. Is this the normal way to debug a program: with a terminal window > open, save changes to the x.rb file in an editor, and enter > > ruby "filename" args > > in the terminal? (Presumable, using a Ruby IDE is another alternative. > I've also noted how irb works.) Yes, this is a pretty typical work sequence. > If "yes", does the execution begin with > the first statement that is outside a class definition (as there seems > to be nothing comparable to C's main(), for example)? > Actually, execution begins with the very first line of non-comment code. Even class definitions are actually executable code. Try putting a "puts" somewhere in a class definition, but outside of a method. > 2. Suppose I've defined two classes, C1 and C2, a "main" program and > some methods that are outside class definitions. Assume there is a > statement > > c1 = C1.new > > in a class C2 method and > > c2 = c2.new > > in a method outside the two classes. In the file containing my code, > maze_problem.rb, do these blocks of code have to be placed in any > particular order? > Consider what I said above about code execution. Beyond that, whether or not the method outside of any classes will work depends on when it is called, rather than when it is defined. > 3. I ran > > ruby maze_problem.rb > > in the terminal and got the following error message: > > undefined method `[]=' for nil:NilClass > > with no reference to where in maze_problem.rb the problem occurred. How > do I go about finding the source of this error? > > Many thanks. > > Cary > Are you sure that is the entire error message? In any case, look for a place where you are setting an element on a variable using a[blah] = something. -Justin