How to write a Ruby gem that can be invoked without the word 'ruby' preceding it - ruby

I would like to write a CLI application wrapped into a Gem that can be invoked the same way git commands are invoked, or gem commands. Eg when running say "git clone " you don't need to precede it with 'ruby'. However, the tutorials and articles I've seen so far about writing gems, don't show this. The examples either require you to run your gem through irb, with appropriate requires, or you run it like 'ruby '. This is not what I want. If you know of any tutorials that cover this, then that would be great.
Thanks.

The "#!" line at the start of a script tells your shell which executable to execute the script with. In this case, it tells it to find the Ruby executable from the environment and give the script to it for execution.
By means of example, I have a file called "hi", with the following:
#!/usr/bin/env ruby
puts "hi!"
I make it executable:
$ chmod a+x hi
Then I can execute it directly, without explicitly invoking the Ruby interpreter:
$ ./hi
hi!
Per the tutuorial you would simply provide such a file which requires your gem and whatnot, and provide it in the executables property of your gemspec:
Gem::Specification.new do |s|
# ...
s.executables << 'hi'
When the gem is installed, the hi script would be installed into a location discoverable on the path, so you could then invoke it.

Related

how to distribute a ruby script via homebrew

How can I deploy a simple ruby script via homebrew?
Here's what I tried
Wrote formula in a GitHub repo named homebrew-foo
# file https://github.com/foo/homebrew-foo/blob/master/foo.rb
class Foo < Formula
desc "A command line tool"
url "https://github.com/foo/foo/archive/master.zip"
version "5.0.1"
def install
bin.install "foo"
lib.install Dir["lib/*"]
end
end
The other repository contains the ruby script. These are the files
./foo
./lib/libfile1.rb
here's what the script does
#!/usr/bin/env ruby
require './lib/libfile1.rb'
puts "came here"
The problem is that the require fails.
$ brew install foo/foo/foo
$ foo
results in this error
/Users/user1/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in
require': cannot load such file -- ./lib/libfile1.rb (LoadError)
from
/Users/user1/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in
require' from /usr/local/bin/foo
$ which foo
/usr/local/bin/foo
I suspect it's because the .rb file is not there at /usr/local/bin/foo/lib/libfile1.rb
Any ideas whats the proper way to do this?
There are two issues with your script:
The first one is you try to require some file relatively to the current directory; i.e. the one from which the script is run, not the one it’s located in. That issue can be fixed by using Ruby’s require_relative:
#!/usr/bin/env ruby
require_relative './lib/libfile1.rb'
puts "came here"
The second issue is the script assumes the lib/ directory is located in its directory; which it’s not because your formula installs the script under <prefix>/bin/ and the library files under <prefix>/lib/. Homebrew has a helper for that use-case called Pathname#write_exec_script. It lets you install everything you need under one single directory, then create an executable under bin/ that calls your script.
Your formula now looks like this:
class Foo < Formula
desc "A command line tool"
url "https://github.com/foo/foo/archive/master.zip"
version "5.0.1"
def install
libexec.install Dir["*"]
bin.write_exec_script (libexec/"foo")
end
end
It installs everything under libexec/ (lib/ is usually reserved for lib files), then add an executable under bin/ that calls your libexec/foo script.
I found the answer to my own question, actually it's a technique used by someone on the net, basically do something like this
#!/usr/bin/env ruby
DBMGR_HOME = File.expand_path('../..', __FILE__)
$LOAD_PATH.unshift(File.join(DBMGR_HOME, 'lib'))
require 'dbmgr'
And the recipe can be like this:
https://github.com/callahanrts/homebrew-dbmgr/blob/master/dbmgr.rb

In a Ruby gem, is it possible to have executables written in other languages?

I have a gem which has an executable in bin/. A wildcard is used in the gemspec:
s.executables = Dir["bin/*"].map &File.method(:basename)
If I have a ruby file at bin/my_file which had chmod a+x run, then it's found in the PATH in bash:
#!/usr/bin/env ruby
puts "working ruby script"
However I have another executable written in coffeescript at bin/my_file.coffee:
#!/usr/bin/env coffee
console.dir "printing from coffee file"
When I run my_file.coffee in shell, it tries to read this script with ruby instead of coffeescript.
How can I run this executable with coffeescript as intended?
As it stands, I'm using a workaround which is to wrap my coffee code as a string in my ruby app and then passing it to coffee -e and capturing the output.
Also, looking at this RubyGems issue shows the official explanation:
RubyGems is not meant to support arbitrary executables.
OK. So is there an alternative besides wrapping code in heredoc?
Is there a good reason this restriction is in place?
I guess it's simply in the specification of RubyGems:
https://github.com/zzak/rubygems/commit/709c5aae7ffd9958cc2ea89dc2caf6b7e02c56b7
+ # Executables included may only be ruby scripts, not scripts for other
+ # languages or compiled binaries.
+ #

Ruby script: shebang with absolute path to ruby not working

I am using Mac OS X. I have two versions (2.1.5 and 2.0.0) of Ruby installed. The former installed at /another/.path/to/ruby (there is a dot before "path" to mimic the fact that the path contains a dot-headed directory in between), in addition to the default system one (version 2.0.0) at /usr/bin/ruby. I used rbenv to install Ruby.
After I manually set the PATH environment variable so the default ruby command will be found in another directory: /another/.path/to/ruby. Now I check
which -a ruby
It is using correct ruby first, as output.
/another/.path/to/ruby
/usr/bin/ruby
Now I create a script, rbs, with the first line of shebang specifying the ruby to use.
#!/usr/bin/env ruby
puts 'hey there'
Then I run
./rbs
it outputs 'hey there'. Good. Meanwhile, the Ruby is using the correct version.
/usr/bin/env ruby --version
as well as
ruby --version
both output 2.1.5. So it does great job to use the new version.
However, here is where the problem occurs: now I update rbs file to be:
#!/another/.path/to/ruby
puts 'hey there'
Note that I updated the shebang to use the absolute path to the desired ruby. then I run
./rbs
It outputs:
./rbs: line 2: puts: command not found
which is too weird;
but if I run
ruby ./rbs
it outputs 'hey there' as normal. It looks like the shebang works perfect using /usr/bin/env ruby, but not for absolute path for the newly install ruby?
Why is this? Is there a way to fix it so the updated script can still work by typing the following?
./rbs
Thanks!
The puts: command not found message indicates that your script is not being run by Ruby, but by the shell instead. So first, I would double-check your shebang line's syntax and path.
Second, note that rbenv uses shims that dynamically find and run the right version of ruby (and associated programs like gem, etc). But the shims are scripts, and scripts can't themselves be shebang interpreters; you have to find and use the actual path to the ruby executable (as output by rbenv which ruby).
On the other hand, since /usr/bin/env is an executable, you can always use something like #!/usr/bin/env ruby, which will work even if the ruby it finds in the path is itself a script.
I can't comment, (otherwise I'd add as a comment) but I think its worthwhile to add that the
#!/usr/bin/env ruby
MUST be the first line of the file. This tripped me up for a while.
source

Ruby Script to Command Line Tool

I am trying to make a command line tool. I already have a ruby script (one file). But I want to project it as a normal command line command.
Right now I have to go into the directory where the script is and type ruby script.rb for it to function but i want to make a command such as script [option] from directory and it should process the required option in the script.
Do I need to make an independent ruby gem for this? I have read about some gems like thor and commander but I am not able to use them properly.
How can I make this command line tool?
PS: An example can be the twitter gem and a command line tool 't' which is also a gem.
Ruby, because it's a general-programming language, makes it easy to create command-line scripts. Here's a basic script you can build upon:
#!/usr/bin/env ruby
require 'optparse'
args = {}
OptionParser.new do |opt|
opt.on('-i', '--input FILE', 'File to read') { |o| args[:file_in] = o }
opt.on('-o', '--output FILE', 'File to write') { |o| args[:file_out] = o }
end.parse!
abort "Missing input or output file" unless (args[:file_in] && args[:file_out])
File.write(args[:file_out], File.read(args[:file_in]))
Here's what's happening:
#!/usr/bin/env ruby is commonly called a "bang line". The shell will look for this line at the top of a file to determine what application can read the file and execute/interpret it. env is an application that will look through the user's PATH environment variable and return the first Ruby found as the Ruby to execute the script. Using this makes the script work with Rubies in the normal /usr/bin, /usr/local/bin or when managed by rbenv or RVM.
require 'opt parse' pulls in Ruby's command-line parser class OptionParser, which makes it easy to set up traditional flags, such as -i path/to/file/to/read, -o path/to/file/to/write, or long parameters, like --input or --output. It also automatically supplies the -h and --help flags to return formatted help text for the script. OptionParser is a bit of a learning-curve, so play with the complete example and you'll figure it out.
The rest should be pretty self-explanatory.
Traditionally, executables that are installed by the system go in /usr/bin. Executables we write, or add, go in /usr/local/bin, and I highly recommend sticking with that.
Some OSes don't automatically supply an entry for /usr/local/bin in the PATH, so you might need to modify your PATH setting in your ~/.bashrc, ~/.bash_profile or ~/.profile to allow the shell to locate the script.
Executable scripts need to have their executable flag set: chmod +x /path/to/executable is the basic command. See man chmod for more information.
I tend to leave the script's extension in place; Ruby scripts are "foo.rb", Python are "bar.py", etc. I do that because I prefer to have that extension as a hint of the language it's written in, but YMMV. The extension isn't necessary so go with what works for you.
Beyond all that, you might want to provide logging output, or output to the system's syslog. In the first case use Ruby's built-in Logger class, or the Syslog class in the second case.
Actually there's two great gems for command line apps in Ruby.
First is methadone which is for simpler command line apps.
Another is gli which is for apps with multiple commands, for example something like bundler.
If you want to know more, you can check out book about creating command line apps build awesome command-line apps in ruby by author of these gems.
You do not need to make it a gem, the following suffices:
Change its name from script.rb to script
Add #!/usr/bin/env ruby as the first line of script
Put it somewhere in PATH (e.g. $HOME/bin, making sure it is in PATH), or execute by giving path explicitly, e.g. $HOME/myscriptdir/script

How to execute a Ruby script in Terminal?

I've set everything up that I need on my Mac (Ruby, Rails, Homebrew, Git, etc), and I've even written a small program. Now, how do I execute it in Terminal? I wrote the program in Redcar and saved it as a .rb, but I don't know how to execute it through Terminal. I want to run the program and see if it actually works. How do I do this?
Just call: ruby your_program.rb
or
start your program with #!/usr/bin/env ruby,
make your file executable by running chmod +x your_program.rb
and do ./your_program.rb some_param
Open your terminal and open folder where file is saved.
Ex /home/User1/program/test.rb
Open terminal
cd /home/User1/program
ruby test.rb
format or test.rb
class Test
def initialize
puts "I love India"
end
end
# initialize object
Test.new
output
I love India
Assuming ruby interpreter is in your PATH (it should be), you simply run
ruby your_file.rb
To call ruby file use : ruby your_program.rb
To execute your ruby file as script:
start your program with #!/usr/bin/env ruby
run that script using ./your_program.rb param
If you are not able to execute this script check permissions for file.
Just invoke ruby XXXXX.rb in terminal, if the interpreter is in your $PATH variable.
( this can hardly be a rails thing, until you have it running. )
For those not getting a solution for older answers, i simply put my file name as the very first line in my code.
like so
#ruby_file_name_here.rb
puts "hello world"
Although its too late to answer this question, but still for those guys who came here to see the solution of same problem just like me and didn't get a satisfactory answer on this page, The reason is that you don't have your file in the form of .rb extension. You most probably have it in simple text mode. Let me elaborate.
Binding up the whole solution on the page, here you go (assuming you filename is abc.rb or at least you created abc):
Type in terminal window:
cd ~/to/the/program/location
ruby abc.rb
and you are done
If the following error occurs
ruby: No such file or directory -- abc.rb (LoadError)
Then go to the directory in which you have the abc file, rename it as abc.rb
Close gedit and reopen the file abc.rb. Apply the same set of commands and success!
In case someone is trying to run a script in a RAILS environment,
rails provide a runner to execute scripts in rails context via
rails runner my_script.rb
More details here:
https://guides.rubyonrails.org/command_line.html#rails-runner
Open Terminal
cd to/the/program/location
ruby program.rb
or add #!/usr/bin/env ruby in the first of your program (script tell that this is executed using Ruby Interpreter)
Open Terminal
cd to/the/program/location
chmod 777 program.rb
./program.rb
You can run ruby code just passing -e option
ruby -e 'x = Time.now; puts x;'
Output will be:
2022-06-22 15:55:06 +0500

Resources