require command problem

P

Pen Ttt

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

Brian Candler

Pen said:
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=> ["/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/<appname>-<ver>/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/...)
 
P

Pen Ttt

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'?
 
J

Jean-Julien Fleck

Hello,
Would you be kind enough to solve this =A0problem for me ?
1.why =A0in my system
/var/lib/gems/1.8 and =A0/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: =A0require =A0'hello' =A0not
require =A0'/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,

--=20
JJ Fleck
PCSI1 Lyc=E9e Kl=E9ber
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top