Ruby Package Include Problems - ruby

I'm trying to use the Optiflag package in my Ruby code and whenever I try to do the necessary require optiflag.rb, my program fails with the standard no such file to load -- optiflag message. I added the directory with that library to my $PATH variable, but it's still not working. Any ideas?

is it a gem? Are you doing
require 'rubygems'
require 'optiflag'
or equivalent?

It looks like it's a gem, so you need to enable ruby gems before requiring it.
This site explains many ways of how to do it. But to have the cheat sheet here these are:
1) Require the rubygems package before using a gem.
require "rubygems"
require "optiflag" # etc
2) Add the -rubygems flag to wherever you execute ruby. I.e:
ruby -rubygems Something.rb
3) Add an environment variable called RUBYOPT, giving it an option of rubygems. I.e:
RUBYOPT=rubygems

I also keep having this problem with RubyXL, tried to use single and double quotes. Is there something else that needs to be done? Maybe putting a file somewhere? I already succesfully installed the gem with sudo gem install rubyXL (RubyXL actually din't work).

Related

When is the 'require' necessary when using a ruby gem?

I noticed for some gems you must include it in the file where you want to use it like this require 'a_gem', but this is not always the case.
I am going to compose a gem by myself. What should I do if I do not want to add the require 'my_gem' to the .rb file when using it?
Usually, an application that is using a gem needs to require the gem:
require "my_awesome_gem"
MyAwesomeGem.do_something_great
However, if an application is using bundler, which defines the application's gem in a file called "Gemfile":
source 'http://rubygems.org'
gem 'my_awesome_gem'
then the application may invoke bundler in a way that automatically requires all of the gems specified in the Gemfile:
require "bundler"
Bundler.require
MyAwesomeGem.do_something_great
Rails projects use Bundler.require, so a Rails application will not need to explicitly require a gem in order to use it: Just add the gem to the Gemfile and go.
For more about Bundler.require, see the bundler documentation
For more about how Rails uses Bundler, see How Does Rails Handle Gems? by Justin Weiss.
This doesn't make sense. If you want to write a Gem and use it, it needs to be required somewhere.
This "somewhere" could be explicit in one of your scripts, it could be written in a Gemfile or it could be required by another script/gem that is required by your script.
If you write a gem, Ruby will not include it automatically in all your scripts.
Finally, if you publish it, should every single Ruby script on earth magically require it and execute your code?
I suppose that the project you have seen which didn't use require 'a_gem' was using a Gemfile.

How to install a ruby gem

I'm trying to write a program with colorized output. I looked at a few, and the gem I found was colorize. I've done some research, but I can't find step-by-step instructions on how to install a gem, require it, and use it. The gem I want is colorize, and I also need to know if there's anything else it requires. All I have is the standard ruby console that comes in the Ruby21 file, and notepad++ to write and save it. I need to know how and where to install it, whether I type something into the terminal or download a file and put it somewhere, and how to require it and its prerequisites(if any) in a file.
You can install the gem using your CLI simply by typing: gem install colorize. You can then utilize the gem by requiring it, so at the top of your .rb file add require 'colorize'. Then just test it out by trying puts "This is blue".colorize(:blue).
Your .rb could look like this for example:
require 'colorize'
puts "This is blue".colorize(:blue)
Per http://guides.rubygems.org/rubygems-basics/, you can install the colorize gem with the following command (in terminal or at command prompt):
gem install colorize
To list local gems, run this command:
gem list
And--by adding require 'colorize' to the top of the respective .rb file--that gem's lib directory will be added to your $LOAD_PATH.

Require local gem ruby

I have a gem that I created in Ruby on my local machine and I need to require this gem in a plain Ruby script that starts a service.
I have to require like this:
require_relative '../../../my-gem/lib/my/gem'
Is it possible to do this require without putting in the relative path?
require checks for files in $LOAD_PATH. You can put your gem in one of those directories in order to require it directly. If you don't like your load path, you can add a new directory to it in your script, or set the RUBYLIB environment variable which is added to the load path.
If you have a gem, you can install it and set the version you want (in my example 1.0.0.beta) with gem 'my-gem', '= 1.0.0.beta'.
But I think yu look for another solution:
You can extend the location where require looks:
$:.unshift('../../../my-gem/lib')
require('my/gem')
or
$LOAD_PATH.unshift('../../../my-gem/lib')
require('my/gem')
You could also use $: << '../../../my-gem/lib', but I prefer unshift. If your gem contains a file with similar names as in a gem (avoid it!), then unshift guarantees your script is loaded.

Difference between gem and require (require open-uri)

I just wanted to understand for my self.
I'm using the nokogiri gem (for parsing HTML). If I got it right to open URLs I need to use a method from the gem 'open-uri'.
But when I include it in my Gemfile (on Windows developer's machine):
gem 'open-uri' - there is an error while bundle install that it can not find gem.
So If I use require 'open-uri' - its working.
So can some explain what is going on?
You're using bundler for your gem dependecies and you're doing it right but OpenUri is part of the Ruby standard library. That's why you only need to require it if you want to use it in your code.
require is used to load another file and execute all its statements. This serves to import all class and method definitions in the file. require also keeps track of which files have been previously required so it doesn't execute it twice.
A RubyGem is a software package, commonly called a “gem”. Gems contain a packaged Ruby application or library. The RubyGems software itself allows you to easily download, install, and manipulate gems on your system.
- What is a Gem?:
The Gemfile is then used by bundler to install the specified gems.
open-uri is not a gem but part of the Ruby Standard Library so it just needs to be required.

Using installed gems in Ruby with 'require'

I do something like sudo gem install json. Then I do irb. Then I do require 'json'. Then it says no such file to load -- json
You need to make sure that RubyGems itself is loaded, for requiring gems to work.
There are several ways to do this. You could require 'rubygems' explicitly in each file you want to use the gems in, but that might get to be a pain. Or you could pass -rubygems into ruby when you execute it, but again, you'd need to remember to do that each time.
The best way would probably be to set the RUBYOPT environment variable to rubygems. For instance, you may add the following line to your .profile:
export RUBYOPT=rubygems
Try adding this first:
require 'rubygems'
RubyGems is a dependency manager as well as an installer.

Resources