0
0
Rubyprogramming~5 mins

Running scripts with ruby command

Choose your learning style9 modes available
Introduction

You use the ruby command to run Ruby programs saved in files. It tells your computer to read and execute the Ruby code.

You wrote a Ruby program and want to see it work.
You want to test a small Ruby script quickly.
You need to run a Ruby script from the command line.
You want to automate tasks using Ruby scripts.
You are learning Ruby and want to try your code in a file.
Syntax
Ruby
ruby filename.rb

Replace filename.rb with your Ruby file's name.

Make sure you are in the folder where the file is saved or provide the full path.

Examples
This runs the Ruby script named hello.rb in the current folder.
Ruby
ruby hello.rb
This runs the Ruby script located at /home/user/scripts/test.rb using the full path.
Ruby
ruby /home/user/scripts/test.rb
This runs example.rb with warnings enabled to help find possible issues.
Ruby
ruby -w example.rb
Sample Program

This simple Ruby script prints a greeting message.

Run it by typing ruby greet.rb in your terminal.

Ruby
# Save this as greet.rb
puts "Hello, Ruby!"
OutputSuccess
Important Notes

You need Ruby installed on your computer to use the ruby command.

If you get an error, check your file name and your current folder.

You can add #!/usr/bin/env ruby at the top of your script and make it executable to run it without typing ruby.

Summary

The ruby command runs Ruby scripts saved in files.

Use it in the terminal with your file name to see your Ruby code work.

Make sure your file is saved and you are in the right folder before running.