Require command problem

i have write a class ,use command to put in the
/usr/lib/ruby/1.8/rubygems/Webmovie.rb
sudo cp /tmp/Webmovie.rb /usr/lib/ruby/1.8/rubygems/Webmovie.rb
Code.rb is my calss ,also in /usr/lib/ruby/1.8/rubygems/

pt@pt-laptop:~$ irb
irb(main):001:0> require ‘Code’
=> true
irb(main):002:0> require ‘Webmovie’
LoadError: no such file to load – Webmovie
from (irb):2:in `require’
from (irb):2
from :0
irb(main):003:0> require ‘/usr/lib/ruby/1.8/rubygems/Webmovie’
=> true
irb(main):004:0>

how can i use command require ‘Webmovie’ to get the class work?

On 2010-06-11 04:04:46 -0700, Pen T. said:

LoadError: no such file to load – Webmovie
from (irb):2:in `require’
from (irb):2
from :0
irb(main):003:0> require ‘/usr/lib/ruby/1.8/rubygems/Webmovie’
=> true
irb(main):004:0>

how can i use command require ‘Webmovie’ to get the class work?

Your filenames should not use uppercase letters. You should not move
things into the rubygems directory manually. You should access them
from within your library or from an installed gem. Look at any Ruby
library to see correct directory structure, file names and use of
require.

Pen T. wrote:

i have write a class ,use command to put in the
/usr/lib/ruby/1.8/rubygems/Webmovie.rb

But /usr/local/lib/1.8/rubygems is not in the load path (i.e. ruby
doesn’t look for files there), so ruby won’t find it.

$ irb --simple-prompt

$LOAD_PATH
=> ["/usr/local/lib/site_ruby/1.8",
“/usr/local/lib/site_ruby/1.8/x86_64-linux”, “/usr/local/lib/site_ruby”,
“/usr/lib/ruby/vendor_ruby/1.8”,
“/usr/lib/ruby/vendor_ruby/1.8/x86_64-linux”,
“/usr/lib/ruby/vendor_ruby”, “/usr/lib/ruby/1.8”,
“/usr/lib/ruby/1.8/x86_64-linux”, “.”]

On your system, /usr/local/lib/site_ruby is probably the appropriate
place to put ad-hoc libraries. Otherwise, your program can choose the
location:

$LOAD_PATH.unshift “/path/to/my/lib”

rubygems itself does add things to $LOAD_PATH, but they typically are of
the form /usr/lib/ruby/gems/1.8/gems/-/lib. If you want
your library to go here, then package it up as a gem, and install the
gem. (For Ubuntu, with rubygems installed using apt-get, it’ll be
/var/lib/gems/1.8/…)

1.hello.rb
position: /home/pt/test/hello.rb
content:
class Hihi
def self.hello
puts “HELLO”
end
end
2.hello.gemspec
position: /home/pt/test/hello.gemspec
content:
Gem::Specification.new do |s|
s.name = ‘hello’
s.version = ‘0.1.0’
s.summary = ‘hello gems’
s.files = ["/home/pt/test/hello.rb"]
s.summary=‘output hello’
s.description=‘output hello’
end
3.gem build
pt@pt-laptop:~$gem build /home/pt/test/hello.gemspec
WARNING: no author specified
WARNING: no email specified
WARNING: no homepage specified
WARNING: no rubyforge_project specified
Successfully built RubyGem
Name: hello
Version: 0.1.0
File: hello-0.1.0.gem
pt@pt-laptop:~$ gem install hello
WARNING: Installing to ~/.gem since /var/lib/gems/1.8 and
/var/lib/gems/1.8/bin aren’t both writable.
WARNING: You don’t have /home/pt/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
ERROR: Error installing hello:
attempt to install file into “/home/pt/test/hello.rb”
pt@pt-laptop:~$ irb
irb(main):001:0> require ‘hello’
LoadError: no such file to load – hello
from (irb):1:in `require’
from (irb):1
from :0
irb(main):002:0> require ‘/home/pt/test/hello’
=> true
irb(main):003:0> Hihi.hello
HELLO
=> nil
irb(main):004:0>

problem:
Would you be kind enough to solve this problem for me ?
1.why in my system
/var/lib/gems/1.8 and /var/lib/gems/1.8/bin aren’t both writable.
how to make them writable?
2.how can i use require command this way: require ‘hello’ not
require ‘/home/pt/test/hello’?

Hello,

Would you be kind enough to solve this problem for me ?
1.why in my system
/var/lib/gems/1.8 and /var/lib/gems/1.8/bin aren’t both writable.
how to make them writable?

You have to be root to write there:

sudo gem install hello

2.how can i use require command this way: require ‘hello’ not
require ‘/home/pt/test/hello’?

You have to require rubygems first for your ruby to know where to look
for gems:

require ‘rubygems’
require ‘hello’

Cheers,

Pen T. wrote:

 s.files = ["/home/pt/test/hello.rb"]

ERROR: Error installing hello:
attempt to install file into “/home/pt/test/hello.rb”

Don’t use absolute path specifications in your gemspec.

I suggest you find a small gem, and copy what they do. That’s what I did
when I wrote my first gemspec:

(sorry, I can’t remember which small gem I based this on :slight_smile:

Regards,

Brian.