Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Beginner's Beginner

Reply
Thread Tools

Beginner's Beginner

 
 
william nelson
Guest
Posts: n/a
 
      04-11-2011
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:uzzle.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/.

 
Reply With Quote
 
 
 
 
Phillip Gawlowski
Guest
Posts: n/a
 
      04-11-2011
On Mon, Apr 11, 2011 at 12:20 PM, Gerard Cahill <(E-Mail Removed)> 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).


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.

 
Reply With Quote
 
 
 
 
Albert Schlef
Guest
Posts: n/a
 
      04-11-2011
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/.

 
Reply With Quote
 
Phillip Gawlowski
Guest
Posts: n/a
 
      04-11-2011
On Mon, Apr 11, 2011 at 2:55 PM, Albert Schlef <(E-Mail Removed)> wrote:
>
> 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.

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      04-11-2011
[Note: parts of this message were removed to make it a legal post.]

On Mon, Apr 11, 2011 at 7:55 AM, Albert Schlef <(E-Mail Removed)>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/.
>
>

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

 
Reply With Quote
 
Railsfid
Guest
Posts: n/a
 
      04-11-2011
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?=

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-11-2011
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:uzzle.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/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      04-11-2011
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help a beginner!!! =?Utf-8?B?U2Ft?= Wireless Networking 4 04-21-2005 06:50 AM
Beginner can't get a wireless VPN connect? Russell DeMarco Wireless Networking 2 03-18-2005 10:35 PM
Beginner can't connect Laptop to wireless network Russell DeMarco Wireless Networking 2 03-17-2005 03:31 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM



Advertisments