Beginner's Beginner

Discussion in 'Ruby' started by william nelson, Apr 11, 2011.

  1. 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/.
    william nelson, Apr 11, 2011
    #1
    1. Advertising

  2. On Mon, Apr 11, 2011 at 12:20 PM, Gerard Cahill <> 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.
    Phillip Gawlowski, Apr 11, 2011
    #2
    1. Advertising

  3. 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/.
    Albert Schlef, Apr 11, 2011
    #3
  4. On Mon, Apr 11, 2011 at 2:55 PM, Albert Schlef <> 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.
    Phillip Gawlowski, Apr 11, 2011
    #4
  5. william nelson

    Josh Cheek Guest

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

    On Mon, Apr 11, 2011 at 7:55 AM, Albert Schlef <>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).
    Josh Cheek, Apr 11, 2011
    #5
  6. william nelson

    Railsfid Guest

    Ruby 1.9.2 difference newbie question

    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?=
    Railsfid, Apr 11, 2011
    #6
  7. william nelson

    7stud -- Guest

    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/.
    7stud --, Apr 11, 2011
    #7
  8. william nelson

    7stud -- Guest

    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/.
    7stud --, Apr 12, 2011
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Smith

    Trivial resources problem (beginner)

    Larry Smith, Oct 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    505
    Scott Manson
    Oct 2, 2003
  2. tripwater

    Help with Visual Studio (beginner)

    tripwater, Feb 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,450
    Amit Bahree
    Mar 9, 2005
  3. Scottie T
    Replies:
    2
    Views:
    4,627
  4. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    557
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  5. Rensjuh
    Replies:
    7
    Views:
    947
    Mabden
    Sep 2, 2004
Loading...

Share This Page