Newbie needs help getting started

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 P.ming 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.) 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)?

  1. 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?

  1. 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

There are numerous ways to accomplish this. You can debug through
various IDEs (komodo for instance), through IRB, through rescues/alerts,
or custom exceptions. I would start here:

As you are learning, I would work on very simple programs and post your
code on gist.github.com as a public gist, and link it here if you have
issues. The quickest way for someone to help you isolate something is
by viewing your code.

Cary Swoveland wrote:

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.

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.

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

Cary Swoveland wrote:

AB & Justin,

Thank you both for your helpful comments. I’m slowly getting there, but
there have been numerous surprises (one, for example, is that ‘x’ is not
a character).

Not having an IDE, I’ve resorted to inserting umpteen puts “…” in my
code to see what’s happening, and to locate the source of Ruby error
messages that do not have a line reference (exceptions, in part, I
think). Am I missing something on debugging technique?

Yes! Use the ruby-debug gem. Just put “debugger” in the code where you
want a breakpoint.

Also remember that test-first development tends to reduce the need for a
debugger, though it certainly does not eliminate it.

I’d really like to use an IDE with a step-through debugger. Can someone
recommend one that runs on Macs?

Don’t use an IDE. Ruby isn’t Java and doesn’t need the heavy IDE
support. Just use a good editor such as KomodoEdit or TextMate.

One other question (that I should have asked initially): is this forum
appropriate for posting newbie questions, or would another be more
appropriate?

All Rails questions are appropriate here.

Cary

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Cary Swoveland wrote:
[…]

One other question (that I should have asked initially): is this forum
appropriate for posting newbie questions, or would another be more
appropriate?

All Rails questions are appropriate here.

Whoops! I forgot which list I was on. All Ruby questions are
appropriate here.

Cary

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

AB & Justin,

Thank you both for your helpful comments. I’m slowly getting there, but
there have been numerous surprises (one, for example, is that ‘x’ is not
a character).

Not having an IDE, I’ve resorted to inserting umpteen puts “…” in my
code to see what’s happening, and to locate the source of Ruby error
messages that do not have a line reference (exceptions, in part, I
think). Am I missing something on debugging technique?

I’d really like to use an IDE with a step-through debugger. Can someone
recommend one that runs on Macs?

One other question (that I should have asked initially): is this forum
appropriate for posting newbie questions, or would another be more
appropriate?

Cary

Cary Swoveland wrote:
[…]

One easy tech question follow.

class Node
attr_reader :row
def initialize(r)
row = r
end
end

n = Node.new(0)
puts “n = #{n}”

I expected
n = 0
but instead, Ruby says
n = #Node:0x100156480
?

Why would you expect n to be 0 when you’re assigning the actual Node
object to n?

Cary

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marnen Laibow-Koser wrote:

Yes! Use the ruby-debug gem. Just put “debugger” in the code where you
want a breakpoint.

Will do, Marnen. Thanks.

Don’t use an IDE. Ruby isn’t Java and doesn’t need the heavy IDE
support. Just use a good editor such as KomodoEdit or TextMate.

I’m using the trial version of TextMate. I’m impressed, but a little
overwhelmed with its features.

All Rails questions are appropriate here.

Did you mean all Ruby q’s are welcome. I’m not using Rails (yet).

One easy tech question follow.

class Node
attr_reader :row
def initialize®
row = r
end
end

n = Node.new(0)
puts “n = #{n}”

I expected
n = 0
but instead, Ruby says
n = #Node:0x100156480
?

Cary

Cary Swoveland wrote:

One easy tech question follow.

class Node
attr_reader :row
def initialize®
row = r
end
end

n = Node.new(0)
puts “n = #{n}”

I expected
n = 0
but instead, Ruby says
n = #Node:0x100156480
?

Cary

You probably want to print n.row , which returns nil in your code
because of a previous error: row = r should be @row = r.

When I was really new to ruby it paid off to write out the setters and
getters by hand. It got boring so fast I happily switched to the
attr_accessor tricks, but with better understanding.

hth,

Siep

Marnen Laibow-Koser wrote:

I expected
n = 0
but instead, Ruby says
n = #Node:0x100156480
?

Why would you expect n to be 0 when you’re assigning the actual Node
object to n?

It would have helped if I didn’t mis-write my little example. What I
meant to write was:

class Node
attr_reader :row
def initialize®
row = r
end
end

n = Node.new(0)
puts “n.row = #{n.row}”

After reading the other replies, I see my error was in line 4: rather
than
row = r
I needed
@row =r

I get it, though I don’t know why
attr_reader :row
is not be written
attr_reader :@row
but I do note that the latter produces an error.

Thanks, all.

Cary

On Jan 19, 2010, at 4:18 PM, Siep K. wrote:

You probably want to print n.row , which returns nil in your code
because of a previous error: row = r should be @row = r.

It took me a while to absorb Ruby’s assignment/setter syntax:

tmp = 1 # local variable assignment
@tmp = 1 # instance variable assignment (bypasses setter)
self.tmp = 1 # method call to method named ‘tmp=’ (i.e. the setter)
# same as self.tmp=(1)

self[index] = 1 # method call to method named ‘[]=’ for current object
# same as self.[]=(index, 1)

tmp[index] = 1 # method call to method named ‘[]=’ for tmp
# same as tmp.[]=(index, 1)

Gary W.

On 19.01.2010 20:48, Cary Swoveland wrote:

Not having an IDE, I’ve resorted to inserting umpteen puts “…” in my
code to see what’s happening, and to locate the source of Ruby error
messages that do not have a line reference (exceptions, in part, I
think). Am I missing something on debugging technique?

Well, #inspect and #class are interesting tools to do debugging,
especially coupled with “puts”, or “p” for a more “raw” output.

I’d really like to use an IDE with a step-through debugger. Can someone
recommend one that runs on Macs?

NetBeans should, and so should Komodo Edit (not sure about its debugging
support, but it should support the ruby-debugger gem).

I’d advise you to grab NetBeans’ Ruby distribution (comes bundled with a
recent-ish JRuby version, even), since it simplifies the overwhelming
nature of most IDEs, at least a little, and supports basically any Ruby
version you can find, as long as you tell NetBeans where yours lives.

One other question (that I should have asked initially): is this forum
appropriate for posting newbie questions, or would another be more
appropriate?

It certainly is appropriate. :slight_smile: