Including Modules in Ruby - 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.

Related

Require in 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.

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 class with pure Ruby

I'm with the following problem:
I'd like to require a 'config/application.rb' file to my index.rb.
In my task, I have to use pure Ruby.
config/application
Dir["app/models/*.rb"].each do |file|
require_relative file
end
Dir["app/importers/*.rb"].each do |file|
require_relative file
end
index.rb
require 'config/application'
contas_endereco = ARGV[0].to_s
transacoes_endereco = ARGV[1].to_s
conta_arquivo = Arquivo.new(contas_endereco)
transacoes_arquivo = Arquivo.new(transacoes_endereco)
transacoes_importer = TransacoesImporter.new(conta_arquivo, transacoes_arquivo)
transacoes_importer.importar
But I got this error:
/home/kelvin/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- config/application (LoadError)
from /home/kelvin/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from index.rb:1:in `<main>'
I tried to use require_relative too, but I got the following error:
/home/kelvin/workspace-ruby/desafio-dinda/config/application.rb:2:in `require_relative': cannot load such file -- /home/kelvin/workspace-ruby/desafio-dinda/config/app/models/transacao.rb (LoadError)
from /home/kelvin/workspace-ruby/desafio-dinda/config/application.rb:2:in `block in <top (required)>'
from /home/kelvin/workspace-ruby/desafio-dinda/config/application.rb:1:in `each'
from /home/kelvin/workspace-ruby/desafio-dinda/config/application.rb:1:in `<top (required)>'
from index.rb:1:in `require_relative'
from index.rb:1:in `<main>'
require 'config/application'
In Ruby, files included with Kernel#require must generally be in the $LOAD_PATH or given as relative paths, and don't include the filename extension. Some examples include:
Add the script's directory to your $LOAD_PATH.
$:.unshift File.dirname(__FILE__)
require 'config/application'
Require a file relative to the current working directory.
require './config/application'
You might also use Kernel#load with an absolute path. For example:
load '/path/to/config/application.rb'

Loading files from a different partition

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?

Trying to Run Ruby script in terminal but get ERROR

I am trying to run a ruby script to rename some files but I get the following error:
Eccleshall$ ruby rename.rb
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- ftools (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from rename.rb:45:in `<main>'
Thanks for the help.
Change to script to say:
require "fileutils"
instead of
require "ftools"
ftools has been deprecated.

Resources