Require in ruby - ruby

I have the following ruby code:
require 'locationclass'
I have installed the gem locationclass, but it's still giving me the error message:
LoadError: cannot load such file -- locationclass
from C:/Ruby/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/Ruby/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):1
from C:/Ruby/bin/irb.cmd:19:in `<main>'
Does anyone know how to fix this?

require command loads files, not gems. As I see the gem "locationclass" has only one file in its lib folder, named main.rb. So to load it you need to call:
require 'main'
Also, it's a bad style to have different names for gem and its main file. Not to mention that name 'main' is too generic.

Related

Ruby SketchUp LoadError if requiring correct files

I have the problem that I want to require a ruby file.
File overview:
ExcelConversion.rb
main.rb
/classes/excelReader.rb
/classes/elementIdentifier.rb
the main.rb has the header:
require classes/excelReader
require classes/elementIdentifier
the excelReader.rb has the header:
require 'rubyXL
the elementIdentifier.rb has the header:
require 'inifile
The problem is that if I execute the main.rb file, everything works fine. But, as soon as I write require main within the ExcelConversion file I get the error:
Error Loading File ExcelConversion.rb
Error: #<LoadError: cannot load such file -- rubyXL>
C:/Program Files (x86)/SketchUp/SketchUp 2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in 'require'
C:/Program Files (x86)/SketchUp/SketchUp 2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in 'require'
C:/shortenedPath/ExcelConversion/classes/ExcelReader.rb:1:in '<top (required)>'
C:/shortenedPath/ExcelConversion/main.rb:1:in 'require_relative'
C:/shortenedPath/ExcelConversion/main.rb:1:in '<top (required)>'
C:/shortenedPath/ExcelConversion.rb:4:in 'require_relative'
C:/shortenedPath/ExcelConversion.rb:4:in '<top (required)>'
I don't understand how that is even possible to throw an error, since the main.rb runs without problems.
Edit: Solution
I managed to find the solution. SketchUp needs to install the gems separately. This can be achieved through the Ruby Console and the command:
Gem.install "nameOfTheGem"
Then SketchUp stores a copy of the gem within it's own path and one can require it as usual.

require 'rubygems' and requre 'RedCloth' is not working. How can I fix it?

I'm learning Ruby from "Beginning Ruby from Novice to Professional" by Peter Cooper. I am on Chapter 7. I have ruby 2.0 installed. I am facing this error:
C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- 2.0/redcloth_scan (LoadError)
When trying to run this code:
require 'rubygems'
require 'RedCloth'
r = RedCloth.new("This is a *test* of _using RedCloth_")
puts r.to_html
I'm getting this result:
ruby redcloth.rb
C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- 2.0/redcloth_scan (LoadError)
Couldn't load 2.0/redcloth_scan
The $LOAD_PATH was:
C:/Ruby200/lib/ruby/gems/2.0.0/gems/RedCloth-4.2.9-x86-mingw32/lib
C:/Ruby200/lib/ruby/gems/2.0.0/gems/RedCloth-4.2.9-x86-mingw32/lib/case_sensitive_require
C:/Ruby200/lib/ruby/gems/2.0.0/gems/RedCloth-4.2.9-x86-mingw32/ext
C:/Ruby200/lib/ruby/site_ruby/2.0.0
C:/Ruby200/lib/ruby/site_ruby/2.0.0/i386-msvcrt
C:/Ruby200/lib/ruby/site_ruby
C:/Ruby200/lib/ruby/vendor_ruby/2.0.0
C:/Ruby200/lib/ruby/vendor_ruby/2.0.0/i386-msvcrt
C:/Ruby200/lib/ruby/vendor_ruby
C:/Ruby200/lib/ruby/2.0.0
C:/Ruby200/lib/ruby/2.0.0/i386-mingw32
from C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/RedCloth-4.2.9-x86-mingw32/lib/RedCloth.rb:13:in `<top (required)>'
from C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:135:in `require'
from C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
from C:/Ruby200/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:144:in `require'
from redcloth.rb:2:in `<main>'
Exit code: 1
I already installed ruby gems and RedCloth. They didn't give me an errors. If somebody could shed some light on this topic it would be greatly appreciated.
try this:
goto into C:/Ruby200/lib/ruby/gems/2.0.0/gems/RedCloth-4.2.9-x86-mingw32/lib
and create a dir named '2.0'.
Then copy redcloth_scan.so from 'C:/Ruby200/lib/ruby/gems/2.0.0/gems/RedCloth-4.2.9-x86-mingw32/lib' into '2.0' dir.
I installed in past RedCloth with Ruby 1.9.1.
Actually I suspect that RedCloth doesn't work with Ruby 2.0.
On ruby 2.x.x you need to compile the gem.
For example for the windows ruby 2.2.4:
Setup the DevKit (https://github.com/oneclick/rubyinstaller/wiki/Development-Kit)
Run into command window:
gem install RedCloth --platform=ruby
Goto to your installed RedCloth path
lib\ruby\gems\2.2.0\gems\RedCloth-4.2.9\lib
Create there a new directory called "2.2" and copy the file redcloth_scan.so on it.
If you use the gem RedCloth-4.2.9-x86-mingw32, the redcloth_scan.so contains the ruby 1.9 dependency and it doesn't works with ruby 2.2.4.

Errors in the Sinatra 'hello world'

Doing the getting started of Sinatra.
I get this error:
./sinatra.rb:5: undefined method `get' for main:Object (NoMethodError)
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /usr/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from sinatra.rb:3
Googling on these errors returns ruby LoadError: cannot load such file which I don't see how that relates to Sinatra.
Not sure what other info I need to share to make my question clearer. So just tell me what other commands I should run to make the question clear.
UPDATE: Actual code
# sinatra.rb
require 'rubygems'
require 'sinatra'
get '/' do
'hey girl'
end
The problem here is due to you naming your file sinatra.rb. When you run that file, the first thing it does is require 'sinatra', and since the current directory is on the load path in Ruby 1.8.7, it tries to load itself. It then gets to the call to get '/' do ..., but since the real Sinatra hasn’t been loaded this results in the error.
The fix is to rename your file to something other than sinatra.rb, you could use myapp.rb as suggested in page you linked to.

Correct way to require files within a RubyGem?

I used Bundler to generate a Gem skeleton for me. Within lib/foo.rb, I have the following:
require 'foo/client'
require 'foo/other'
Those two lines are supposed to require lib/foo/client.rb and lib/foo/other.rb, respectively. It builds without a problem, but when I go to test it with irb, I get a file not found error.
ruby-1.9.2-head :003 > require 'foo'
LoadError: no such file to load -- foo/client
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/gems/ruby-1.9.2-head/gems/foo-0.1.0/lib/foo.rb:3:in `<top (required)>'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):3
from /home/ethan/.rvm/rubies/ruby-1.9.2-head/bin/irb:16:in `<main>'
ruby-1.9.2-head :004 >
What is the correct way to require files within the same Gem? There must be something simple that I'm overseeing...
If your gem is called 'foo', then all you need to do is use bundle exec:
bundle exec your-script.rb
Without bundle exec, the load paths are not set up correctly.
Using irb, you use the bundle command bundle console.
chris#chris:~/oss/pp-adaptive$ irb
irb(main):001:0> AdaptivePayments
NameError: uninitialized constant Object::AdaptivePayments
from (irb):1
from /home/chris/.rbenv/versions/1.9.2-p290/bin/irb:12:in `<main>'
irb(main):002:0>
chris#chris:~/oss/pp-adaptive$ bundle console
irb(main):001:0> AdaptivePayments
=> AdaptivePayments
irb(main):002:0>
Note that once the gem is installed on your system, you may use it without bundler.
The current directory is not in the load path in Ruby 1.9. Try one of these:
require './client'
or
require_relative 'client'
If you are in IRB itself you may have to
require "rubygems"
require "foo"
if the library is a gem. Alternatively you can require the full path of the gem, but I wouldn't advise it since rubygems does require magic so reload! works in irb ( at least it does for rails console ).

Including Modules in Ruby

I am trying to include a Ruby module.
In the file helper.rb, I have this text
module Helper
...
end
In the file test.rb, I have this text:
....
require 'helper'
...
These files are on the same level of the directory yet I keep getting this error:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- helper (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from test.rb:4:in `<main>'
I have also tried
include Helper
in test.rb and get this error:
test.rb:4:in `<main>': uninitialized constant Object::Helper (NameError)
What am I doing wrong?
In Ruby 1.9 you should use
require_relative 'helper'
Try require './helper'. That should do it.

Resources