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'm using Ubuntu. I'm trying to run a ruby file todo.rb
I added this shebang as the very first line of the file
#!/usr/bin/env ruby
I go to the directory where the rb file is located and then run todo.rb and get error todo.rb: command not found.
So I went directly to the /usr/bin directory. I found the env command and ran it. The output of the env command displays ruby paths and ruby data:
MY_RUBY_HOME=/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290
PATH=/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290/bin:/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290/bin:/home/tallercreativo/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
GEM_PATH=/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290:/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290#global
RUBY_VERSION=ruby-1.9.2-p290
So since, I couldn't make it work, I changed the shebang to point to ruby directly:
#!/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
and I get same command not found error. What's wrong?
You need to first make your script executable:
chmod +x todo.rb
then you need to run it like so:
./todo.rb
You cannot run it by just saying todo.rb, unless you place it in your PATH, in which case you can do so from anywhere.
You're missing the ruby at the end of your env command. Did you mean:
#!/usr/bin/env ruby
You need to tell env what executable you're looking for.
(Question Updated)
Are you executing your code like this? todo.rb ? You either need to provide the full path to your script (/home/you/project/todo.rb) or a relative path (./todo.rb) unless that directory is inside your $PATH.
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 found rspec in /Library/Ruby/Gems/1.8/gems/rspec-2.6.0/lib
I even put a path to it in my .bash_profile:
PATH="/usr/local/bin:/Library/Ruby/Gems/1.8/gems/rspec-2.6.0/lib:
But when I enter rspec in terminal I get command not found. Even when I'm in the same directory as the file rspec?
Besides being a noob, what am I doing wrong?
*Update:*I was able to execute rspec using "bundle exec rspec" but I'd like to figure out why I can't just use rspec.
Check that your PATH environment variable is what you expect. echo $PATH
Check that your target binary has executable permission. ls -l /Library/Ruby/Gems/1.8/gems/rspec-2.6.0/lib
Check again. which rspec
for a file to be executable as a binary you generally need to:
(a) make the file executable by running chmod 755 /path/to/scripts/awesometask.gem
(b) add a "shebang" to the top of the file (first line) that contains the path of the interpreter for the script: #!/usr/bin/ruby
(c) execute file with full path: /path/to/scripts/awesometask.gem
or
(c1) add the folder where the file is to PATH: PATH=$PATH:/path/to/scripts