i want to execute my ruby script as executable file and also i should execute at /usr/bin/ directory. I know it is possible like this.
#!/usr/bin/ruby
puts "hello"
And
chmod +x hello
But I also want to require some ruby file.
For example if I add
require './other_ruby_script'
into my codes and I move the Ruby executable file to /usr/bin/, it gives me error for:
cannot load such file 'other_ruby_script'
I want to execute the Ruby file at /usr/bin directory.
So maybe I should compile it? But I couldn't compile because i didn't understand when google searches "How to compile?".
How can i create executable ruby code as suitable format for my codes. (require './other_file'). And i don't have to execute like this ./hello my executable file. Just i should execute as hello
You can make the script executable with the following command: chmod +x hello. rb . chmod is a shell command that allows us to change the permissions for a file. The +x specifies that the script should be executable.
ocra. OCRA (One-Click Ruby Application) builds Windows executables from Ruby source code. The executable is a self-extracting, self-running executable that contains the Ruby interpreter, your source code and any additionally needed ruby libraries or DLL.
#!/usr/bin/env ruby
require_relative 'other_ruby_script'
puts "hello"
I think you ask how to configure the right loadpath. First, in your script I would do a:
puts $:
This should print whether you are loading the right Ruby environment (might be a problem if you are using rbenv or rvm). For example I get:
/Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/site_ruby/1.9.1
/Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.3.0
/Users/pmu/.rbenv/versions/1.9.3-p194/lib/ruby/site_ruby
As long as your loadpath does not contain the directory with the script 'other_ruby_script' you will get this error:
LoadError: cannot load such file -- other_ruby_script
So, you should try to add the load path with:
$:.unshift "#{File.dirname(__FILE__)}/../some_path"
If you are not loading the Ruby environment in the first place, your line:
#!/usr/bin/ruby
needs to be setup to load the environment from Rbenv or Rvm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With