I just wondering can we turn a command line tool that written in ruby to a gem file ? if yes how we can run that gem ?
think I have a ruby file which print a text very simple app
now I want to package it to a gem file
then I need to run that gem
Is this doable or not ?
This is perfectly possible. RubyGem has a guide on this. Basically you'll need to:
Apart the gem structure, you'll want to see how to add an executable to it by creating a file in the bin folder with something like:
#!/usr/bin/env ruby
require 'yourmainfile'
# call your code here
Related
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.
I am currently working on developing a gem and I would like to be able to run that gem from command line and point it to my local source. Previously I've done this in rails by specifying the path in the gem file but now I would like to run the gem on a non rails app. So I would like to know how to call the gem from command line and specify that I want to use the source code in a certain directory.
For example can I cd into the directory that I want to run it on and do something like: ruby my_gem --path=~/code/mygem
Also do I have to build them gem or can I run it from source without building it?
From the root directory of your gem, try this to execute lib/MyGem.rb:
ruby -Ilib lib/MyGem.rb
or test your gem interactive:
irb -Ilib
> require 'mygem'
true
How do gems like "rails", "rspec", and "cucumber" allow user to use commands that start with their gem name??
rails new project
rspec spec
cucumber features
Not all gems have this ability. For example, when I type json even though I have it installed, I get
-bash: json: command not found
Gem's .gemspec file looks like this:
Gem::Specification.new do |s|
s.name = "haml"
s.version = "3.1.8"
....
s.executables = ["haml", "html2haml"]
end
This means that when installing this Gem (haml-3.1.8 in this case) also links to executables (also called "binstubs") will be created for the files haml and html2haml which are found inside the gem's bin/ directory.
In this case, for example the file bin/haml could look like:
#!/usr/bin/env ruby
require 'rubygems'
require 'haml'
puts Haml::VERSION
From rubygems.org documentation on building Gems:
In addition to providing libraries of Ruby code, gems can also expose
one or many executable files to your shell’s PATH. Probably the best
known example of this is rake. Another very useful one is
prettify_json.rb, included with the JSON gem, which formats JSON in a
readable manner (and is included with Ruby 1.9).
[...]
Adding an executable to a gem is a simple process. You just need to
place the file in your gem’s bin directory, and then add it to the
list of executables in the gemspec. Let’s add one for the Hola gem.
[...]
The executable file itself just needs a shebang in order to figure out
what program to run it with.
[...]
All it’s doing is loading up the gem, and passing the first command
line argument as the language to say hello with.
These gems have binaries that can be executed from the CLI. Most gems do not need this functionality and only provide code extensions.
Edit: They may not be 'binaries'. They can be just executable Ruby code as well. Thanks #holger
Is there an easy way, when running a Ruby script, to get it to automatically install gems required for script?
For example, consider these require statements at the top of a ruby script:
require 'net/http'
require 'fileutils'
require 'archive/zip'
Now, I know (as a human and programmer) that for this script to run on a given PC with Ruby, the 'gem install archive-zip' command needs to be run first, before the script will work. But if this script needs to run on dozens of PCs, is there anything that can save me from having to ensure ALL the gem dependancies are installed first?
Furthermore, what if there are several gems required?
Not sure if this is exactly what you are after but when I have a server set up how I want I dump a list of my gems to somewhere safe...
gem list > my_gems.txt
If I need to rebuild the box or build another machine I use this script to install the gems...
bulk_gems.rb
#! /usr/local/bin/ruby
STDIN.readlines.each do |l|
m = l.match /^(\S+) \((.*)\)/
unless m.nil?
gem_name, versions = m[1], m[2].split(',')
versions.each do |v|
system "gem install #{gem_name} --version #{v} --ignore-dependencies"
end
end
end
more my_gems.txt | bulk_gems.rb
By using gem unpack you can unpack the gems into a directory. From there, you can include them in your script. For example, randomly picking the gem morse (a gem that encodes/decodes morse code), let's say I use gem unpack morse to put it in a directory /gems/. It unpacks to the directory morse-0.0.2, since that's the version.
$LOAD_PATH << './gems/morse-0.0.2/lib'
require 'morse'
# The gem is included, and Morse is now defined.
Shoes has a really slick way of doing this. See this blog post by _why.
You could port some of that to standard ruby (without the fancy UI)
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).