Run a Ruby Library from the Command-Line

Run a Ruby library from the command-line

You can find a similar functionality in ruby.

__FILE__ the current source file name.

$0 Contains the name of the script being executed. May be assignable.

source: Ruby Quick Ref

How to execute a published Ruby Gem CLI app

I see some problems with this. But first, let me address your original question.

To test it on the command line (terminal):

$ gem install phl-covid-testing
$ which run
$ run

To test it in your Bundler project, add phl-covid-testing to your Gemspec/Gemfile. Then do this:

$ bundle install
$ bundle exec run

Now for the problems. The first problem you can see above is that you named your script run. This will be put on the user's path. It'd be better to rename your bin/run script to bin/phl-covid-testing or something else.

Next, I don't know if you did chmod +x on your script file in bin/, but I'll assume you did. However looking at the contents of the file, you have this:

#!/usr/bin/env ruby

require "bundler/setup"
require "./lib/environment"

PHLCovidTesting::CLI.new.call

You shouldn't use ./lib/environment, instead just do require 'environment'. You also don't need require "bundler/setup". If you're testing locally, just use bundle exec bin/run.

Next, don't use the name environment.rb. I suggest renaming this file to the name of your Gem, so it would be phl-covid-testing.rb.

Lastly, your repo has a lot of unnecessary files. Remove .DS_Store and phl-covid-testing-0.1.0.gem and also add these to .gitignore:

# .gitignore
.DS_Store
*.gem

There might be more files that you need to ignore or more problems, but these are the major ones that I see at a glance.

EDIT:

I decided to add how you can test this without uploading to RubyGems.org.

If you're using bundler and rake and you have require 'bundler/gem_tasks' in your Rakefile, then you can do this:

$ bundle exec rake install:local

This will build and install the Gem locally.

Else, you can also do this using the gem command manually:

## Remove old gems (optional).
$ rm -v *.gem

$ gem build phl_covid_testing.gemspec
$ gem install *.gem

See the official doc for more information.

Then try to run the script as I mentioned earlier:

$ phl_covid_testing
## Or with bundler:
$ bundle exec phl_covid_testing

How do i run Ruby script from command line ?

Try putting this in filename.rb:

def hi()
puts "hello"
end

hi

Then run your code in the command line: with ruby filename.rb

How can I run a Ruby gem from the shell?

Write Your Own Wrapper Script

You're over-thinking this. A gem is just a packaged Ruby module. Most gems have executable Ruby scripts that allow one to run them from the command line, but some don't, especially if they're intended to be used as libraries instead of commands. If your gem doesn't have its own executable wrapper, you can write your own.

Assuming your gem is called something like "temperature," you could simply write an executable Ruby wrapper named temp.rb like so:

#!/usr/bin/env ruby

require 'temperature'
Temperature.convert_to_celsius ARGV.first

Put the wrapper somewhere in your PATH (e.g. $HOME/bin), and make sure the script is executable (e.g. chmod 755 ~/bin/temp.rb). Then you can call the script from the command line:

$ temp.rb 80
26.6667

how use gem package to run command line in ruby

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

Running RubyGem CLI during development

Normally when you run an executable from a gem Rubygems will set up the LOAD_PATH for you so that it contains your gems’ lib directory. Obviously when you run the script in development Rubygems doesn’t get the chance to set things up for you so you will need to do it yourself.

One way to do this is in the script file itself. For example the Haml and Redcarpet gems do this. Somewhere near the top of your executable you would have something like this:

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

(assuming a fairly typical directory layout).

Another way, if you want to avoid manipulating the load path in code, would be to add the lib dir in the command line. The -I option to the ruby command lets you do this, so you could run:

$ ruby -Ilib/ bin/roar

from the top level of your gem.

Alternatively you could use the RUBYLIB environment variable:

$ RUBYLIB=lib ruby bin/roar

or even (if bin/roar has a shebang line and is executable):

$ RUBYLIB=lib ./bin/roar

You could even export RUBYLIB so you didn’t need to specify it each time, but you would need to be careful about leaving it set if you did that.

command-line ruby scripts accessing a libs folder

At the top of each bin/executable, you can put this at the top

#!/usr/bin/env ruby

$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')
require 'libfile'

[etc.]

Were you looking for something different?

If you turn your application into a Ruby gem and install the gem on your system, you don't even need to put this stuff at the top. The require statement would suffice in that case.



Related Topics



Leave a reply



Submit