0

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
4
  • 1
    Assuming some *nix variant, you'd need to make it executable (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. Commented Jul 20, 2016 at 16:07
  • @DerrellDurrett I doubt you understand how ruby classpath works. Commented Jul 20, 2016 at 16:11
  • There are great libraries for this take a look at thor or slop they can make this task much easier and far more friendly when scope creep causes "Simple" to all of sudden change to "Complex". Commented Jul 20, 2016 at 17:11
  • This is an OS question, not Ruby. The file has to be in the executable path to do what you want. Commented Jul 20, 2016 at 21:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I have created a gem too but I'm not sure how I'd run it as a console application. I'm updating the question with dependencies.
Read how to create executables and ship them with a gem in the article I linked (section “ADDING AN EXECUTABLE”.)
The question remains. How and what should I ship so that it runs on another machine as stated in the question?
A gem. When the gem will be installed on another machine, dependencies will be resolved and the executables will be automatically made available for the execution. Please read the article I linked, instead of asking questions, that are perfectly answered there.
There is no need to copy the file to a directory, just make sure the directory is in the PATH.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.