Beginner's Beginner

W

william nelson

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!
 
P

Phillip Gawlowski

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

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

Albert Schlef

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

Phillip Gawlowski

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

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

Josh Cheek

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

Phillip Gawlowski wrote in post #992098:

My Ruby 1.9 doesn't have a "require_relative" method.
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).
 
R

Railsfid

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?=
 
7

7stud --

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

7stud --

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top