I've got Ruby installed & some Ruby code but don't know how to run it

9 views
Skip to first unread message

dkathrens77

unread,
Feb 28, 2008, 9:35:49 PM2/28/08
to Ruby Brigades
Hi, I"m here looking for a few pointers.

I'm not new to programming, but I'm having trouble figuring out how to
run some ruby code.

It's the Sudoku solver from the O'Reilly book on Ruby. I'd include it
here except for the copyright issues, so here's a link:

http://www.oreilly.com/catalog/9780596516178/toc.html

look for "A Sudoku Solver in Ruby"

I think I understand that this file is only some routines (classes?
methods? ), but
they need an input file to work on, and perhaps another Ruby script to
apply them. This file is
heavily commented, but I still can't understand what to do.

Would anyone here like to enlighten me on this process? I am running
Windows XP, and I can open a command prompt window and start irb ok.
I've even made an irb.rc file to change the way irb starts up.

I've got a couple of sudoku puzzle files to work on. I'd really like
to see this work!

dkathrens77

unread,
Feb 29, 2008, 10:41:13 AM2/29/08
to Ruby Brigades
Hello again.

It's funny how simply writing down a question gets me focused on
answering it myself.

I got myself a little clearer on what "ARGF.readlines" does, so now

puts
Sudoku.solve(Sudoku::Puzzle.new(ARGF.readlines))

makes more sense.

I copied the code lines from the comments of the O'Reilly Sudoku
Solver into
a file named SolveSudoku.rb:

# SolveSudoku.rb
#
# Script to solve a Sudoku puzzle
# sudoku.rb (O'Reilly Sudoku Solver code) must be in same directory

require 'sudoku'
puts Sudoku.solve(Sudoku::Puzzle.new(ARGF.readlines))


Then I opened a command prompt in the directory containing all the
code and
my text files containing Sudoku puzzles to be solved.

At the command line I typed in

> ruby SolveSudoku.rb "Easy01.txt"

(the argument "Easy01.txt" is a file containing an incomplete Sudoku
puzzle)

The argument tells SolveSudoku.rb what to look at and solve.

I got an error about "Duplicates in Puzzle" so I went back and read
everything again.

In my text file I was putting zeroes (0) to indicate an unknown cell
in the puzzle,
but actually a period (.) is required.

The code saw more than one zero in a line and threw an exception, just
like the code says it will.

One little "Aha!" moment is all it takes to begin learning. My whole
life is a series of "Aha!" moments.

Jon Garvin

unread,
Feb 29, 2008, 12:41:17 PM2/29/08
to Ruby-B...@googlegroups.com
For future reference, you probably want to ask questions like this on
Ruby Talk mailing list. You'll get a better response there. The Ruby
Brigades list is simply for discussing setting up and managing local
ruby users groups. But congrats on figuring out a solution to your
problem anyway.

http://www2.ruby-lang.org/en/20020104.html

dkathrens77 wrote:
> One little "Aha!" moment is all it takes to begin learning. My whole
> life is a series of "Aha!" moments.
>
>
>


--
http://www.5valleys.com/
http://www.workingwithrails.com/person/8078

Reply all
Reply to author
Forward
0 new messages