Beginner's Beginner

I am trying to execute the Sudoku Solver listed in the book "The Ruby
Programming Book" (pages 17 -24)

The code for the Sudoku Module is available online but I do not
understand the following instruction:

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

Can someone walk me through (baby-steps) how to actually "require"
sudoku so that I can see the Module in action. The more explicit your
answer the better since I am a newbee's newbee!

···

--
Posted via http://www.ruby-forum.com/.

By requiring Sudoku, you're loading the contents of the file sudoku.rb which
could contain several modules or classes (or even just one module by the sound of things).

The next line outputs (puts) the result of the method 'solver', which is a
class method of Sudoku. The parameter Sudoku::Puzzle.new(ARGF.readlines) is a
statement that creates a new instance of Sudoku Puzzle, passing ARGF.readlines
as a parameter to the constructor (a.k.a the initialize method).

ARGF is a constant that represents the parameters passed into the program from
the command line (usually). I took this definition from (http://blog.bogojoker.com/2008/11/data-and-argf-in-ruby/)
"ARGF takes each of the elements in ARGV, assumes they are filenames, and allows you to process these files as single stream of input"

So if you've passed muliple files into the program from the command line, ARGF represents them as one big file. The method readlines, reads each line of ARGF into an array.

Keep reading that book and all will be revealed :slight_smile:

william nelson wrote in post #992087:

I am trying to execute the Sudoku Solver listed in the book "The Ruby
Programming Book" (pages 17 -24)

The code for the Sudoku Module is available online but I do not
understand the following instruction:

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

Can someone walk me through (baby-steps) how to actually "require"
sudoku so that I can see the Module in action. The more explicit your
answer the better since I am a newbee's newbee!

When you require() a file, ruby looks in some default directories for
the file. You can see which directories are searched by default like
this:

puts $LOAD_PATH

The default directoies that are displayed should also include the
current directory ("."), which is the directory specified in the prompt
next to which your typed the command to run your program:

some_dir/sub_dir> ruby my_program.rb

So if the file you are trying to include is in the current directory, or
one of the default directories, you can require() it.

If the file you are trying to require() is in some other directory, then
you need to either move it into the current directory or one of the
default directories; or you can tell ruby to also search the directory
which contains the file you are trying to require().

There are several ways to accomplish that:

1) Add the directory containing the file you want to require() to the
directories that are searched by default:

$LOAD_PATH << "/some_dir/sub_dir/my_ruby_programs/"

For a more permanent solution, you can also:

2) Add the directory containing the file you want to require, to your
system's PATH environment variable.

3) Create a new environment variable called RUBYPATH, which specifies
additional directories that you want ruby to search.

···

--
Posted via http://www.ruby-forum.com/\.

And a Gotcha!:

Ruby 1.9 expects* you to use

require_relative "path/to/file_sans_rb"

if you want to require something you wrote yourself.

Example**:

PS C:\Sourcery\popbuilder> dir

    Directory: C:\Sourcery\popbuilder

Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 27/12/2010 09:08 bin
d---- 27/12/2010 09:08 doc
d---- 14/01/2011 09:48 lib
d---- 27/12/2010 09:08 test
-a--- 27/12/2010 12:00 601454 log.txt
-a--- 27/12/2010 10:18 32 popbuilder.rb

PS C:\Sourcery\popbuilder> dir lib

    Directory: C:\Sourcery\popbuilder\lib

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 27/12/2010 11:18 848 kingdom.rb
-a--- 27/12/2010 09:56 899 namegen.rb
-a--- 27/12/2010 12:56 1668 towns.rb

PS C:\Sourcery\popbuilder> cat .\popbuilder.rb
require_relative "lib/kingdom"

I haven't dug into the rationale behind this in detail (my guess is to
avoid security implications of arbitrary file loading, as well as
namespace pollution), but there you go. It's also a nice clue that you
are dealing with your own fault, rather than a library you can blame
bugs on. :wink:

* I know that there are workarounds for this, or that you can define
your own require methods, but let's stay simple.

** Something entirely random, but where I used relative_require. These
are not the details you are looking for. *waves hand*

···

On Mon, Apr 11, 2011 at 12:20 PM, Gerard Cahill <gerard.cahill@gmail.com> wrote:

By requiring Sudoku, you're loading the contents of the file sudoku.rb which
could contain several modules or classes (or even just one module by the sound of things).

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

7stud -- wrote in post #992150:

1) Add the directory containing the file you want to require() to the
directories that are searched by default:

$LOAD_PATH << "/some_dir/sub_dir/my_ruby_programs/"

$LOAD_PATH is an array, and the << is like calling push() on an
array--for instance:

arr.push(additional_element)

The result is that the $LOAD_PATH array will contain an additional
string, which is the name of the directory you want to add. However,
the $LOAD_PATH array will be changed only for the duration of your
program; when you run another program, the $LOAD_PATH array will be
reconstructed with the default directories plus whatever directory is
the current directory.

···

For a more permanent solution, you can also:

2) Add the directory containing the file you want to require, to your
system's PATH environment variable.

3) Create a new environment variable called RUBYPATH, which specifies
additional directories that you want ruby to search.

--
Posted via http://www.ruby-forum.com/\.

Phillip Gawlowski wrote in post #992098:

Ruby 1.9 expects* you to use

require_relative "path/to/file_sans_rb"

if you want to require something you wrote yourself.

My Ruby 1.9 doesn't have a "require_relative" method.

···

--
Posted via http://www.ruby-forum.com/\.

Mine does.

However:
PS C:\Sourcery\> irb
irb(main):001:0> RUBY_VERSION
=> "1.9.2"

···

On Mon, Apr 11, 2011 at 2:55 PM, Albert Schlef <albertschlef@gmail.com> wrote:

My Ruby 1.9 doesn't have a "require_relative" method.

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.

The alternative is:
require "#{File.dirname __FILE__}/path/to/file_sans_rb"

I usually use this, because require_relative is so new (and undocumented)

Though someone on IRC suggest that if you do this, then you're doing
something wrong, your environment should be set up to prevent this from
being necessary (I assume they mean for libs and projects, not necessarily
for 1 off scripts).

···

On Mon, Apr 11, 2011 at 7:55 AM, Albert Schlef <albertschlef@gmail.com>wrote:

Phillip Gawlowski wrote in post #992098:
> Ruby 1.9 expects* you to use
>
> require_relative "path/to/file_sans_rb"
>
> if you want to require something you wrote yourself.

My Ruby 1.9 doesn't have a "require_relative" method.

--
Posted via http://www.ruby-forum.com/\.

Is there a difference between ruby 1.9.2 and ruby 1.9.2dev? and if so how do I update it to just ruby 1.9.2?