0
0
RubyHow-ToBeginner · 3 min read

How to Run a Ruby Program: Simple Steps for Beginners

To run a Ruby program, save your code in a file with a .rb extension and use the command ruby filename.rb in your terminal. This executes the Ruby interpreter to run your program and show the output.
📐

Syntax

To run a Ruby program, use the command line syntax:

  • ruby: This calls the Ruby interpreter.
  • filename.rb: This is the Ruby file you want to run.

Make sure your Ruby file has the .rb extension and is saved in your current directory or provide the full path.

bash
ruby filename.rb
💻

Example

This example shows a simple Ruby program that prints a greeting message. Save it as hello.rb and run it using the command ruby hello.rb.

ruby
puts "Hello, Ruby!"
Output
Hello, Ruby!
⚠️

Common Pitfalls

Common mistakes when running Ruby programs include:

  • Not saving the file with a .rb extension.
  • Running the command from a different directory than where the file is saved.
  • Typing the filename incorrectly or missing the ruby command.

Always check your current directory with pwd (Mac/Linux) or cd (Windows) and ensure the file exists there.

bash
wrong: ruby hello
right: ruby hello.rb
📊

Quick Reference

CommandDescription
ruby filename.rbRun the Ruby program saved in filename.rb
pwd (Mac/Linux) or cd (Windows)Check current directory
ls (Mac/Linux) or dir (Windows)List files in current directory

Key Takeaways

Save your Ruby code in a file with a .rb extension before running.
Use the command 'ruby filename.rb' in the terminal to run your program.
Make sure you run the command from the directory where your Ruby file is saved.
Check your file name and extension carefully to avoid errors.
Use basic terminal commands to navigate and verify your files.