steady_daddy steady_daddy - 11 months ago 63
Ruby Question

simple command line Ruby application

I have been asked to develop a simple command line Ruby application that retrieves results from an external API with a specified search query. I have created a script to do that and made it executable using

#!/usr/bin/env ruby
. My script name is sampleapp.rb. So I can run it from console as

$> ./sampleapp some_argument

That is only when I am inside the file directory.

In the example given to me, the script/app was supposed to run like this:

$> sampleapp some_argument

I did a lot of research but couldn't find a way to achieve this. Please guide me here.

Also, how can I make this sampleapp global so it can be called from anywhere.



require 'net/https'
require 'openssl'
require 'json'
require 'uri'
require 'cgi'

module Someapp

class SampleApp

def search(query ="")
...some code here ...

if ARGV.size == 0 || ARGV[0].strip == ""
puts "Please provide search query as an argument"
query = ARGV[0].strip


If this script does not depend on any 3rd party library (what is unlikely) and runs smoothly with system ruby, the easiest way would be to copy it to /usr/local/bin folder:

sudo cp ./sampleapp /usr/local/bin/

If it depends on other libraries, the preferred way would be to build a gem out of it, and install it with system ruby. There are many tutorials on how to produce a gem, the canonical one is likely this.