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
Related
Total newb here. I'm trying to get a simple ruby program to run in the terminal on my MacBook Pro. I used Atom text editor to write the following:
class Sample
def hello
puts "Hello, World!"
end
end
s = Sample.new
s.hello
I saved the file as my_program.rb to a folder on my desktop. I go to the terminal to run the program. I type
ruby my_program.rb
and it returns
ruby: No such file or directory -- my_program.rb (LoadError)
I can use the irb and run a single line of ruby using
ruby -e 'puts "hello world"'
But can't get it to find the .rb file.
I appreciate any help y'all can offer! Thanks!
ruby ~/Desktop/my_program.rb
Ruby might be clever, but is has no mind-reading builtin. You need to tell it, where your file is. This is, of course, not Ruby-specific, but applies to all commands - they can't guess, where in your file system you have stored a file.
An alternative would be to place ~/Desktop in your PATH and use
ruby -S my_program.rb
Ruby will then execute the first program with this name which it finds in $PATH. Whether it is wise to place the Desktop directory into the PATH is a different issue....
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.
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
I'm using ruby v1.9.1 in combination with vim and I execute my scripts with:
:!ruby "%"
my scripts are running fine if I add:
$:.unshift File.dirname(__FILE__)
to add the path of this file to the LOAD_PATH of ruby. If I omit this line my require statements to local scripts aren't working anymore.
Is there a way to pass the path of the file to rubys LOAD_PATH? Something like (completly fictional):
:!ruby "%" --add-to-load-path
I did some research before and stubled upon require_relative, but this has the same effect as require and is not working.
You can use the -I option of the ruby executable and write something like the following:
:!ruby -I%:p:h. %
See ruby --help for further information and file modifiers.
Edited: see comments.
I've written a data collection script for Cacti in Ruby and it runs fine from the command line but Cacti runs the script via "env -i" which strips the environment so Ruby can't find the rubygems library ("in `require': no such file to load -- rubygems (LoadError)"). How might I work around this?
#!/bin/sh
#export LOAD_PATH=whatever
#export RUBYLIB=whatever
#export RUBYOPT=whatever
#export RUBYPATH=whatever
#export RUBYSHELL=whatever
#export PATH=$PATH:whatever
exec ruby -x. $0 "$#"
#!/usr/bin/ruby
require 'rubygems'
require 'open4' # or whatever
# rest of ruby script here
This is a shell script that runs ruby with -x, which will cause the interpreter to skip lines until it finds #!.*ruby. This will give you a chance to restore the environment. The . after -x is a noop, you can take out the ., or replace it with a directory. Ruby will cd there before running the script.
I'm actually guessing that this is not really what you want, since this could have been done without any trickery by just making two scripts, one for the shell, one for Ruby. Perhaps the list of environment variables Ruby cares about will help...
I don't think $LOAD_PATH used for gems (at least, not exclusively). You might want to look at a couple environment variables that haven't been mentioned here yet:
ENV['GEM_HOME']
ENV['GEM_PATH']
You can see your current paths for gems with:
require 'rubygems'
puts Gem.path
A partial answer might be here: comp.lang.ruby post
Can you modify any of the following in your Ruby script: $:, $-I or $LOAD_PATH? These all just point to the same array which specifies where Ruby looks for classes and other ephemera...
>> $LOAD_PATH
=> ["/usr/local/lib/ruby/site_ruby/1.8", "/usr/local/lib/ruby/site_ruby/1.8/i686-darwin9.5.0", "/usr/local/lib/ruby/site_ruby", "/usr/local/lib/ruby/1.8", "/usr/local/lib/ruby/1.8/i686-darwin9.5.0", "."]