Require does not work

Hi fellas!

I use ruby 1.9.2, and I try to force working the example from the book
i’m reading “The well-grounded rubyist”:

#stacklike.rb

module Stacklike
def stack
@stack ||= []
end
def add_to_stack(obj)
stack.push(obj)
end
def take_from_stack
stack.pop
end
end

#stack.rb

require “stacklike”

class Stack
include Stacklike
end

s = Stack.new

Both files under modules folder. When I running stack.rb I have the
error:
internal:lib/rubygems/custom_require:29:in require': no such file to load -- stacklike (LoadError) from <internal:lib/rubygems/custom_require>:29:inrequire’
from modules/stack.rb:1:in `’

None of these methods does not work too:
require “modules/stacklike”
require “modules/stacklike.rb”

But
load “modules/stacklike.rb”
works fine!

I’m absolutely noob in ruby and any help is appreciated.

Hi, Tony

Please see the FAQ of Ruby 1.9.2:
http://www.ruby-lang.org/en/news/2010/08/18/ruby-1-9.2-released/#label-7
It says:

It causes a LoadError
$: doesn’t include the current directory. Some script may
need modifications to work properly.

The global variable $: (equivalent to $LOAD_PATH) is the list of load
path, which is used by the require method.
If you want to load your own ruby module which exists in the directory
/path/to/moddir, you need to append the path to $LOAD_PATH before
using the require method.

#stack.rb

append the path into the load path list

$LOAD_PATH << “/path/to/moddir”

require “stacklike”

class Stack
include Stacklike
end

s = Stack.new

Another way to append a path into $LOAD_PATH is to use a command line
option “-I”.

$ ruby -I /path/to/moddir stack.rb

Regards,

Thank you for help pal! Now it works.
Seems in time the book was writing there wasn’t such error and it should
work fine in versions of ruby up to 1.9.2.

Tony Montana wrote:

Thank you for help pal! Now it works.
Seems in time the book was writing there wasn’t such error and it should
work fine in versions of ruby up to 1.9.2.

Ruby 1.9.2 is the first version of Ruby 1.9 to be considered
feature-complete, stable, and ready for production use.

Unfortunately, many books that cover Ruby 1.9 were actually written
for 1.9.1 (which was an interim release) or 1.9.0 (which was a
development release).

However, I don’t think it would be a good idea to use 1.9.1 or 1.9.0
for learning, just for compatibility with the books. Instead, for the
foreseeable future, you’ll just have to check the errata websites for
all books released before, say, the winter of 2010.

jwm

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs