I am trying to create gem using bundler. This gem requires pp gem to make 'pretty print'. I have require 'pp' at the top of the source and after that I use pp where needed. However, a runtime error occurs.
D:/PRJ/git/smde/vendor/bundle/ruby/2.5.0/gems/pp-0.1.1/lib/pp.rb:1:in `require': cannot load such file -- pp/room (LoadError)
There is no room file in lib/pp directory in pp gem. Why?
What is more interestingly, pp gem works well when I start my gem scripts directly, i.e. "ruby myscript.rb". The lack of pp/lib/room is not essential.
The "pp" gem is not required for using pretty print. That gem is related to Campfire, which does have the concept of a room. See https://www.rubydoc.info/gems/pp/0.1.1/Pp
Pretty print is available to you without requiring anything: notice if you run irb, you can immediately type
pp "something"
And it will print as you want.
Related
I am following the gem guide in the bundler documentation.
when i reached the command line part where i have to require 'foodie/cli' on the executable, i keep getting error.
/Users/suyesh/.rbenv/versions/2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- foodie/cli (LoadError)
from /Users/suyesh/.rbenv/versions/2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from exe/foodie:2:in `<main>'
here is my code in the executable
#!/usr/bin/env ruby
require 'foodie/cli'
Foodie::CLI.start
here is my cli which is in lib/foodie/cli.rb
require 'thor'
require 'foodie'
module Foodie
class CLI < Thor
desc "portray ITEM", "Determines if a piece of food is gross or sdeliciour."
def portray(name)
puts Foodie::Food.portray(name)
end
end
end
What am i doing wrong?
Depending on how you call your executable and how your paths are set up it should work (tm).
Use bundle exec path/to/my/executable in your development environment (assuming you are writing a gem - which is nearly always a good idea). If you followed the other steps in the Bundler tutorial I believe you should have a gemspec file which tells bundler where to look for ([lib/]foodie/cli.rb).
Use ruby -Iinc/lude/dir/for/example/lib path/to/my/executable to tell your ruby to (temporarily) also look in the directory inc/lude/dir/for/example/lib for source files (in your case foodie/cli.rb). Since you are learning bundler, dont use this approach, but it might be interesting to others, and to cast some light in the area where lib-, load- and include-paths live.
I'm trying to use this this gem, but even though it shows up with a gem list after I install it, a require 'stopwords-filter' in irb results in LoadError: cannot load such file -- stopwords-filter.
To make sure that I was actually able to install and use gems, I also tried installing this gem and I can require it just fine. Everything works.
What am I missing about stopwords-filter?
Thanks!
Even though the gem is named stopwords-filter, the require statement is just stopwords:
require 'stopwords'
I've got a wrapper for my Gem, socks, inside socks.rb. The entire file is made up of require statements, and a module declaration:
# lib/socks.rb
require 'socks/version'
require 'socks/base_controller'
require 'socks/templates'
require 'socks/tasks'
require 'socks/rake_tasks'
module Socks
end
However, require 'socks/tasks' and socks/rake_tasks is giving me a LoadError: no such file to load -- socks/tasks / rake_tasks error.
Is this a problem with the alignment of the require statements, or just the code?
Code is on Github: https://github.com/Beakr/socks
EDIT: require './socks/tasks' is now working, however require './socks/rake_tasks' is not.
Ruby load files using its $LOAD_PATH. This global array is changed by e.g. rubygems and bundler to allow to find libraries in various locations. In your sock.gemspec you have defined
gem.require_paths = ["lib"]
which means that rubygems will add the lib directory of your gem to ruby's $LOAD_PATH. But it odes so only if you have installed the gem and the gemspec is thus evaluated. If you don't want to install your gem, you can test your gem using
bundle exec irb
in your gem directory, or alternatively by first adapting your $LOAD_PATH in your irb session like so:
$LOAD_PATH.push "/path/to/your/gem/lib"
require 'socks'
I'm new to Ruby and I was looking around for libraries to parse html and found Nokogiri. The problem I'm having is if I fire up IRB and type require 'nokogiri', it works fine and prints out true. But if I include that as a part of my .rb file and execute it with ruby <scriptname>, it gives me a LoadError: no such file to load message.
My Ruby version is 1.8.7 if I type ruby --version in the terminal.
Try adding
require 'rubygems'
to the .rb file.
In Ruby 1.9+ rubygem is loaded automatically and not needed. Although there is debate whether that line is required pre 1.9.
Nevertheless, give it a shot.
I have seen many samples of Ruby code with this line (for example, http://www.sinatrarb.com/). What is purpose of this require?
# require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello world!"
end
In all cases the code works without this line.
require 'rubygems' will adjust the Ruby loadpath allowing you to successfully require the gems you installed through rubygems, without getting a LoadError: no such file to load -- sinatra.
From the rubygems-1.3.6 documentation:
When RubyGems is required, Kernel#require is replaced with our own
which is capable of loading gems on demand.
When you call require 'x', this is what happens:
If the file can be loaded from the existing Ruby loadpath, it
is.
Otherwise, installed gems are searched for a file that
matches. If it's found in gem 'y', that gem is activated
(added to the loadpath).
The normal require functionality of returning false if that file
has already been loaded is preserved.
See the documentation for Kernel#require to understand why this is necessary.
It is often superfluous. It will allow you to require specific versions of particular gems though, with the gem command.
https://guides.rubygems.org/patterns/#requiring-rubygems
As an addition to prior (and correct answers): Ruby 1.9 and newer ship with RubyGems built-in, so there is no real need to require 'rubygems'. Source here