I can't get the $PATH to work. What am I doing wrong not understanding?
192:~ me$ echo $PATH
/User/me/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
192:~ me$ ruby rtest
ruby: No such file or directory -- rtest (LoadError)
192:~ me$ cd ~/bin
192:bin me$ ruby rtest
Hello World!
why can't it find rtest from anywhere if it's inside my /User/me/bin?
rtest just has 2 lines :
#!/usr/bin/env ruby
puts 'Hello World!'
The $PATH is a list of directories for the shell to search for executables and scripts. In your case, the executable is ruby and rtest is an argument given to ruby. The shell searches only for ruby and runs it, giving rtest to Ruby as a string. Whatever the program does with its arguments is up to to the program. In your case, it is the name of a file for Ruby to find. With no directory name given, it looks in the current directory for a file named rtest. If it's there, it runs it, if not it complains. In your home directory, there is no such file, so ruby complains. When you change to the directory where the file is, it can find it in the current directory, so it runs it.
Try putting the rtest in ~/bin and make it executable:
chmod +x ~/bin/rtest
The first line of your script (#!/usr/bin/env ruby) tells the shell how to run it. You should be able to run it from anywhere:
$ rtest
(Instead of ruby rtest.) It won't execute the script unless you have permission to execute it.
I have a ruby script with following content:
#!/data1/thirdparty/ruby/bin/ruby -I/data1/thirdparty/ruby/lib/ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/gems/2.0.0/gems/ruby-net-ldap-0.0.4 -I/data1/thirdparty/ruby/lib/ruby/site_ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/2.0.0/i686-linux
When I ran the script it throws the following error:
<internal:gem_prelude>:1:in `require': cannot load such file -- rubygems.rb (LoadError)
from <internal:gem_prelude>:1:in `<compiled>'
I took an strace of the program and found this in the strace:
open("/data1/thirdparty/ruby/lib/ruby/2.0.0 -I/data1/thirdparty/enc/encdb.so", O_RDONLY|O_LARGEFILE) = -1 ENOENT (No such file or directory)
So it seems like ruby is not able to handle -I properly because it is including it in the file path itself. How can I force the script to use -I as an include path directive?
Try this one:
#!/bin/bash
exec /data1/thirdparty/ruby/bin/ruby -I/data1/thirdparty/ruby/lib/ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/gems/2.0.0/gems/ruby-net-ldap-0.0.4 -I/data1/thirdparty/ruby/lib/ruby/site_ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/2.0.0/i686-linux -x "$0" "$#"
#!ruby
p "this is my ruby code"
I would work with rvm and bundler. You can then call your script with a rvm-wrapper (for example if run in a cron job), or with bundle exec for development. You will need to have your *.gemspec well configured (for an example run bundle new mygem and look at mygem.gemspec).
The other possibility would be to write a shell script including
/data1/thirdparty/ruby/bin/ruby -I/data1/thirdparty/ruby/lib/ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/gems/2.0.0/gems/ruby-net-ldap-0.0.4 -I/data1/thirdparty/ruby/lib/ruby/site_ruby/2.0.0 -I/data1/thirdparty/ruby/lib/ruby/2.0.0/i686-linux <yourfile.rb>
.
Btw. the #! line is called a shebang or hashbang, in case you want to research what happens.
I'm trying to use a .rb file from within a shell script like so:
ruby file.rb "input data"
In file.rb (it's in the root of a rails app), it requires another file which is throwing an error when I try the chmod method. Any suggestions?
Doing ruby file.rb "input data" outside of the .sh file works completely fine.
I've tried the answers here: Run .rb (Ruby) file on a Shell Script, and chmod and adding #!/usr/bin/ruby do not work. Would be grateful for any suggestions.
I'm on a mac, ruby-2.1.4.
When I try to run it, I get:
"/Users/user/.rvm/rubies/ruby-2.1.4/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- bundler/setup (LoadError)"
How about replacing ruby file.rb "input data" with
bundler exec ruby file.rb "input data" ?
You should also check bundle-exec manpage for more information on how bundler will run your script.
I have a simple test file which contains:
#!/usr/local/bin/ruby -w
puts "Hello, Ruby!";
And when I try running it using ruby test.rb I get this error:
ruby: No such file or directory -- test.rb (LoadError)
Make sure you're running it from the same directory the file is in. If it's in a subdirectory, use ruby subdirectory_name/test.rb. If you use the ls command in your terminal, you should see the test.rb file in the directory you are in now.
run like this
ruby test.rb.txt
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