I'm trying to write to an XML file using Builder but cannot seem to find Builder when I start up my program. I installed it using gem install builder and then used the following in my file, but it does not start up:
require 'rubygems'
require_gem 'builder'
def product_xml
builder = Builder::XmlMarkup.new("", 2)
puts builder.person {
name("Selene")
id("1")
x("2");
y("3");
}
end
The error that I am getting is:
LoadError: no such file to load -- rubygems
require at org/jruby/RubyKernel.java:1038
(root) at C:\Users\Ron\Dropbox\...\server\.\data\plugins\cmd-dev\dev.r
b:2
To install gems inside the JRuby enviroment you must use jruby -S gem, not just gem (if you are not using RVM).
So you should install Builder with jruby -S gem install builder. Once you have installed it, you should just use require 'builder', not require_gem 'builder'.
Related
I want to create a Gemfile.lock by typing 'bundle install' but my local machine can't find the gem net/http. I've tried typing 'bundle update net/http' & 'bundle --full-index' & 'gem install bundler' but I keep getting this error when I try 'bundle install' again:
Could not find gem 'net/http' in rubygems repository https://rubygems.org/ or installed locally. The source does not contain any versions of 'net/http'
my Gemfile resembles the following:
source "https://rubygems.org"
gem 'open-uri'
gem 'nokogiri'
gem 'net/http'
gem 'pry'
Other solutions to this problem suggest removing the line for gem net/http because net/http is part of the default library for Ruby...however when I do this everything loads fine, and I can create a Gemfile.lock upon typing 'bundle install' but when I run my code I get the following error:
Traceback (most recent call last):
run.rb:4:in `': uninitialized constant Net (NameError)
Did you mean? Set
The line of code this refers to is
response = Net::HTTP.get(url)
I'm running Ruby version 2.6.1
The name of the gem is net-http. It is one of the first hits when you google for "net/http".
However, in Ruby 2.6.1, net/http is still part of the standard library, not a gem. Net/http was only removed from the standard library in Ruby 3.0.
There are two different kinds of standard gems:
Default gems: These gems are part of Ruby and you can always require them directly. You cannot remove them. They are maintained by Ruby core.
Bundled gems: The behavior of bundled gems is similar to normal gems, but they get automatically installed when you install Ruby. They can be uninstalled and they are maintained outside of Ruby core.
Your problem is the wrong name gem ('net-http' instead of 'net/http', you can run gem search ^net to find out remote gems start by 'net').
If the gem is 'default', no need to declare it in Gemfile.
You can check standard default gems on: https://stdgems.org/
I'm trying to build my first gem. Using Ryan Biggs' tutorial as my guide, I did the following:
1) Created the gem scaffolding:
$ bundle gem hello_world
2) Edited the lib/hello_world.rb file:
require "hello_world/version"
module HelloWorld
def hi
"Hello world!"
end
end
3) Installed the gem via bundler:
$ cd hello_world
$ bundle install
At this point, if I run
$ bundle show hello_world
it shows
/Users/ykessler/gems/hello_world
so it looks like it installed.
But when I try to require the gem from irb:
require '/Users/ykessler/gems/hello_world'
it can't load it:
2.0.0-p195 :003 > require '/Users/ykessler/gems/hello_world'
LoadError: cannot load such file -- /Users/ykessler/gems/hello_world
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):3
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'
Where am I going wrong?
You need to run gem build hello_world.gemspec
Then to install it, you run gem install hello_world from the root of your gem project. That will install your local gem using the .gem file that we just created in your directory (not the gem from rubygems.org if it exists).
Now, if you run gem list, you should see it. You should now be able to require your gem and and access your library from other ruby code. All you have to write is require 'hello_world'. There is no need to type the full path. In fact, that's a bad idea.
This is all explained pretty clearly in the rubygems.org documentation (http://guides.rubygems.org/make-your-own-gem/). It's very clear, helpeful, and it's where I learned how to make my first gem.
I've got a Ruby app using deamon-kit to create a daemon that runs a cron task every 3 seconds.
Problem is I'm trying to add some error checking using Errbit, so that requires me to:
require 'hoptoad_notifier'
in my script. However, the script is complaining it can't find the file?
.rvm/gems/ruby-1.9.2-p320#stitch/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require': no such file to load -- hoptoad_notifier (LoadError)
What confuses me is that the gem is installed, when I run
gem list | grep hoptoad_notifier
I get
hoptoad_notifier (2.4.11)
Another test I did was to pop into irb console, on the same terminal window, after making sure I'm inside the correct RVM gemset off course:
1.9.2p320 :001 > require 'hoptoad_notifier'
=> true
1.9.2p320 :002 >
And voila, hoptoad is loading. It's only when loading my deamon-kit deamon, that I get the error.
What further confuses me is that when I look at my require block:
require 'rubygems'
require 'resque'
require 'hoptoad_notifier'
It's finding rubygems and resque, but not hoptoad_notifier? Why, when I comment out hoptoad, it doesn't also complain about resque and rubygems?
dameon-kit uses bundler, so you don't need to include rubygems. Include the following lines to your Gemfile :
gem 'resque'
gem 'hoptoad_notifier'
Run bundle install
and include your gems as usual :
require 'resque'
require 'hoptoad_notifier'
It worked for me.
I am able to install the json library using ruby gems. In case it's relevant, I'm using the latest OS X.
usr$ sudo gem install json
Password:
Building native extensions. This could take a while...
Successfully installed json-1.6.5
1 gem installed
Installing ri documentation for json-1.6.5...
Installing RDoc documentation for json-1.6.5...
However, attempting to require 'json' it doesn't work. What am I doing wrong?
touakas-MacBook-Pro:tmp jacob$ ruby x.rb
x.rb:3:in `require': no such file to load -- json (LoadError)
from x.rb:2
The x.rb is as follows:
#!/usr/bin/env ruby
require 'json'
x = { "a"=>"b" }
print x.to_json
You need to load RubyGems first:
require 'rubygems'
require 'json'
Edit:
According to the comment below, you'd better use ruby -rubygems x.rb rather than require rubygems directly (suppose that you're writing a module for resue).
Credit to: #injekt and #Ingenu
I'm trying to install the yob pdf reader: https://github.com/yob/pdf-reader#readme
I'm following the directions there and do:
gem install pdf-reader
and that reports success.
I place a pdf in my directory, name it 1.pdf.
Then I create a script that has the following code:
reader = PDF::Reader.new("1.pdf")
when I run that script I get the following error:
uninitialized constant PDF (NameError)
Googling it hasn't been very successful so far.
How do I solve this?
If it's a pure ruby script, you may be missing the following at the beginning:
require 'rubygems'
require 'pdf-reader'
You can also specify the version of the gem that you want to require, in case you have multiple versions installed:
require 'rubygems'
gem 'pdf-reader', "~> 0.10.0"
require 'pdf-reader'