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.
Related
Ubuntu 18.04, ruby 2.5.1p57
I have a ruby program that I use on a number of different linux system. On one it produces a screed of warning from require 'savon'
elasticsearch#secesprd02:~$ ruby /usr/local/tools/dev/es-cluster/bin/send-json.rb -v --cluster test -c /usr/local/tools/dev/conf/conf.json -r name=ES-api-winlogbeat json/winlogbeat-api-key
/var/lib/gems/2.5.0/gems/akami-1.3.1/lib/akami/wsse.rb:99: warning: shadowing outer local variable - key
/var/lib/gems/2.5.0/gems/akami-1.3.1/lib/akami/wsse.rb:99: warning: shadowing outer local variable - v1
/var/lib/gems/2.5.0/gems/akami-1.3.1/lib/akami/wsse.rb:99: warning: shadowing outer local variable - v2
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59: warning: loading in progress, circular require considered harmful - /var/lib/gems/2.5.0/gems/gyoku-1.3.1/lib/gyoku/hash.rb
from /usr/local/tools/dev/es-cluster/bin/send-json.rb:8:in `<main>'
from /usr/local/tools/dev/es-cluster/bin/send-json.rb:8:in `require_relative'
from /usr/local/tools/dev/common-library/lib/app-configure.rb:3:in `<top (required)>'
from /usr/local/tools/dev/common-library/lib/app-configure.rb:3:in `require_relative'
from /usr/local/tools/common-library/lib/SecretServer.rb:1:in `<top (required)>'
from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in `require'
from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `require'
from /var/lib/gems/2.5.0/gems/savon-2.12.1/lib/savon.rb:26:in `<top (required)>'
from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
...
The program runs without any obvious problems apart from the warings.
I have updated savon and akami gems but this made no difference.
The warning is caused due to
loading in progress, circular require considered harmful - /var/lib/gems/2.5.0/gems/gyoku-1.3.1/lib/gyoku/hash.rb
That should not cause you any harm, given that the require keeps track of files already loaded, so the behaviour of your application should not be affected in any way.
A good overview on that can be found on this answer.
Under normal circumstances it's fine to require the same file twice, because require keeps a record of the libraries it has loaded and ignores a second call.
It looks as if you get this error if the library you are requiring
requires itself somehow, which is not something that require can cope
with -- the require operation presumably needs to complete before the
library is added to the list.
So if in a.rb you require b.rb and in b.rb you require a.rb. then when you write require "a" Ruby will
* start to require a.rb
* start to require b.rb
* start to require a.rb again and realise there is something terribly wrong.
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.
I am trying to get test-first-ruby-master as instructed but I am getting multiple errors. This is the structure of the files/directory in test-first-ruby-master:
Gemfile Gemfile.lock README.md lib spec
lib contains ruby files and spec contains spec files.
I am trying to execute 00_hello_spec.rb file on my terminal but it doesn't work.
What I have tried
require "lib/00_hello.rb"
require_relative "lib/00_hello.rb"
Errors I get
supritkumars-MacBook-Pro:test-first-ruby-master supritkumarshah$ rspec spec/00_hello_spec.rb
/Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `require': cannot load such file -- lib/00_hello.rb (LoadError)
from /Users/supritkumarshah/Desktop/test-first-ruby-master/spec/00_hello_spec.rb:103:in `<top (required)>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `load'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `each'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:97:in `setup'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:85:in `run'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:70:in `run'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.2.3/exe/rspec:4:in `<top (required)>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `load'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `<main>'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
from /Users/supritkumarshah/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Thank you
The require is failing because you're requiring a relative path, 'lib/00_hello.rb', but the directory it's relative to (the root of your project) is not in the list of directories that Ruby is looking in for files to require (the load path, $LOAD_PATH).
There are lots of ways to fix this. Here are some:
Tell rspec to add the root of your project to the load path. Since that's your current directory, you can use .:
rspec -I. spec/00_hello_spec.rb
Change your spec to require_relative '../lib/00_hello'. This works regardless of the current directory or load path — a good idea in command-line programs, although not so important for a learning project.
As B Seven answered, change your spec to require './lib/00_hello'. This requires you to run rspec in the root of your project.
You can even remove the require altogether! When you run rspec, if the current directory has subdirectories named lib and/or spec, rspec adds them to the load path. This also requires you to run rspec in the root of your project. This solution won't work if you need the require when running your program for real (not under rspec).
Note that including the .rb in the filename you required did not cause the problem, but it's not necessary.
Although Dave's answer is correct, I prefer to use:
require './lib/00_hello'
Did you tried adding require 'bundler/setup' at the top of your test helper file?
I'm working at C:\(...)\Desktop, and I need to require my scripts at G:\scripts. I tried to change the $LOAD_PATH:
$LOAD_PATH << 'G:\\scripts'
require 'filename'
and also the argument of require:
require 'G:\\scripts\\filename'
but I always get
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- dir_iterator (LoadError)
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from a.rb:2:in `<main>'
It works well if the directory is on C:\ partition. What's happening here?
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.