Beginner's Beginner

Beginner's Beginner

Post by william ne » Tue, 12 Apr 2011 18:26:29


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.yqcomputer.com/
 
 
 

Beginner's Beginner

Post by Gerard Cah » Tue, 12 Apr 2011 19:17:59

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://www.yqcomputer.com/ )
"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 :)

 
 
 

Beginner's Beginner

Post by Phillip Ga » Tue, 12 Apr 2011 19:29:02


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. ;)

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

Beginner's Beginner

Post by Albert Sch » Tue, 12 Apr 2011 21:55:39

Phillip Gawlowski wrote in post #992098:

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

--
Posted via http://www.yqcomputer.com/
 
 
 

Beginner's Beginner

Post by Phillip Ga » Tue, 12 Apr 2011 22:56:21


Mine does.

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

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

Beginner's Beginner

Post by Josh Chee » Tue, 12 Apr 2011 23:49:00

[Note: parts of this message were removed to make it a legal post.]



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

Beginner's Beginner

Post by 7stud - » Wed, 13 Apr 2011 05:04:09

william nelson wrote in post #992087:

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.yqcomputer.com/
 
 
 

Beginner's Beginner

Post by 7stud - » Wed, 13 Apr 2011 08:23:53

7stud -- wrote in post #992150:

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



--
Posted via http://www.yqcomputer.com/