You use the ruby command to run Ruby programs saved in files. It tells your computer to read and execute the Ruby code.
Running scripts with ruby command
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.
hello.rb in the current folder.ruby hello.rb
/home/user/scripts/test.rb using the full path.ruby /home/user/scripts/test.rb
example.rb with warnings enabled to help find possible issues.ruby -w example.rb
This simple Ruby script prints a greeting message.
Run it by typing ruby greet.rb in your terminal.
# Save this as greet.rb puts "Hello, Ruby!"
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.
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.
Practice
ruby command do when used in the terminal?Solution
Step 1: Understand the purpose of the ruby command
Therubycommand is used to run Ruby scripts saved in files from the terminal.Step 2: Differentiate from other Ruby tools
It does not compile code, open the console, or install gems automatically.Final Answer:
It runs a Ruby script saved in a file. -> Option AQuick Check:
ruby command runs scripts = A [OK]
- Confusing ruby command with irb (interactive console)
- Thinking ruby compiles code to executables
- Assuming ruby installs gems automatically
hello.rb from the terminal?Solution
Step 1: Identify the correct command syntax
The correct syntax to run a Ruby script isruby filename.rb.Step 2: Check each option
Onlyruby hello.rbmatches the correct syntax.Final Answer:
ruby hello.rb -> Option DQuick Check:
Use ruby + filename.rb to run scripts [OK]
- Using 'run' or 'execute' commands which don't exist
- Adding extra words like 'start' before ruby
- Forgetting to include the .rb extension
greet.rb with the following content:puts "Hello, Ruby!"
What will be the output when you run
ruby greet.rb in the terminal?Solution
Step 1: Understand the Ruby code
The code usesputsto print the string "Hello, Ruby!" to the terminal.Step 2: Predict the output of running the script
Runningruby greet.rbexecutes the code and prints the string.Final Answer:
Hello, Ruby! -> Option AQuick Check:
puts prints string output = C [OK]
- Thinking puts prints the code itself
- Expecting no output from puts
- Confusing syntax errors with correct code
ruby myscript.rb but get an error: ruby: command not found. What is the most likely cause?Solution
Step 1: Analyze the error message
The errorruby: command not foundmeans the terminal cannot find the ruby program.Step 2: Identify the cause
This usually means Ruby is not installed or its location is not in the system PATH environment variable.Final Answer:
Ruby is not installed or not in your system's PATH. -> Option BQuick Check:
Command not found means Ruby missing or PATH issue [OK]
- Assuming syntax error causes command not found
- Thinking file must be saved to fix this error
- Adding ./ before filename only works for executables, not ruby command
first.rb and second.rb. You want to run second.rb but accidentally type ruby first.rb. How can you avoid this mistake when running scripts?Solution
Step 1: Understand the cause of the mistake
Typing the wrong filename runs the wrong script because the command requires exact filenames.Step 2: Find the best practice to avoid errors
Always verify your current folder and type the exact filename with extension to run the correct script.Final Answer:
Always check your current folder and type the exact filename with extension. -> Option CQuick Check:
Exact filename avoids running wrong script [OK]
- Thinking Ruby auto-selects the right script
- Trying to run scripts without filenames
- Using incorrect commands like 'ruby run'
Ready to prove it?
Pass the test and this pattern moves unknown
