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.
Update:
sampleapp.rb
require 'net/https'
require 'openssl'
require 'json'
require 'uri'
require 'cgi'
module Someapp
class SampleApp
def search(query ="")
...some code here ...
end
if ARGV.size == 0 || ARGV[0].strip == ""
puts "Please provide search query as an argument"
else
query = ARGV[0].strip
SampleApp.new.search(query)
end
end
chmod +x sampleapp), and have it live in a directory that's in your (or more generally the caller's)$PATH. As the already given answer suggests, one way to do this is to put it in/usr/local/bin.thororslopthey can make this task much easier and far more friendly when scope creep causes "Simple" to all of sudden change to "Complex".