How do you run a Ruby script named hello.rb from the command line?
You run it by typing ruby hello.rb in the terminal and pressing Enter.
Click to reveal answer
beginner
What does the ruby command do when running a script?
It starts the Ruby interpreter, reads the script file, and executes the Ruby code inside it.
Click to reveal answer
intermediate
Can you run a Ruby script without the ruby command? If yes, how?
Yes, by adding a shebang line #!/usr/bin/env ruby at the top of the script and making it executable with chmod +x script.rb, then run it with ./script.rb.
Click to reveal answer
beginner
What happens if you run ruby without any script file?
Ruby starts an interactive session called IRB (Interactive Ruby), where you can type Ruby code and see results immediately.
Click to reveal answer
intermediate
How do you pass arguments to a Ruby script when running it?
You add the arguments after the script name like ruby script.rb arg1 arg2. Inside the script, you access them with the ARGV array.
Click to reveal answer
What command runs a Ruby script named app.rb?
Arun app.rb
Bruby app.rb
Cexecute app.rb
Dstart app.rb
✗ Incorrect
The correct command to run a Ruby script is ruby app.rb.
What does the ARGV array contain in a Ruby script?
AThe script's filename
BEnvironment variables
CRuby interpreter version
DCommand line arguments passed to the script
✗ Incorrect
ARGV holds the command line arguments passed to the Ruby script.
How can you make a Ruby script executable without typing ruby before it?
AAdd <code>#!/usr/bin/env ruby</code> at the top and make it executable
BRename it to <code>.exe</code>
CUse <code>chmod 000</code>
DRun it with <code>run script.rb</code>
✗ Incorrect
Adding a shebang line and making the script executable allows running it directly.
What happens if you type ruby alone in the terminal?
AIt runs the last script
BIt shows an error
CIt opens an interactive Ruby session
DIt exits immediately
✗ Incorrect
Typing ruby alone starts IRB, an interactive Ruby shell.
Which of these is NOT a way to run a Ruby script?
Arun script.rb
B./script.rb (with shebang and executable permission)
Cruby script.rb
DUsing IRB to load the script
✗ Incorrect
run script.rb is not a valid command to run Ruby scripts.
Explain how to run a Ruby script from the command line and how to pass arguments to it.
Think about the command format and how the script receives extra words after the filename.
You got /4 concepts.
Describe how to make a Ruby script executable without typing 'ruby' before it.
Consider what goes at the top of the file and what permission you change.
You got /3 concepts.
Practice
(1/5)
1. What does the ruby command do when used in the terminal?
easy
A. It runs a Ruby script saved in a file.
B. It compiles Ruby code into an executable.
C. It opens the Ruby interactive console.
D. It installs Ruby gems automatically.
Solution
Step 1: Understand the purpose of the ruby command
The ruby command 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 A
Quick Check:
ruby command runs scripts = A [OK]
Hint: ruby command runs saved scripts, not compilers or consoles [OK]
Common Mistakes:
Confusing ruby command with irb (interactive console)
Thinking ruby compiles code to executables
Assuming ruby installs gems automatically
2. Which of the following is the correct way to run a Ruby script named hello.rb from the terminal?
easy
A. run hello.rb
B. start ruby hello.rb
C. execute hello.rb
D. ruby hello.rb
Solution
Step 1: Identify the correct command syntax
The correct syntax to run a Ruby script is ruby filename.rb.
Step 2: Check each option
Only ruby hello.rb matches the correct syntax.
Final Answer:
ruby hello.rb -> Option D
Quick Check:
Use ruby + filename.rb to run scripts [OK]
Hint: Use 'ruby filename.rb' to run scripts in terminal [OK]
Common Mistakes:
Using 'run' or 'execute' commands which don't exist
Adding extra words like 'start' before ruby
Forgetting to include the .rb extension
3. Given a file greet.rb with the following content:
puts "Hello, Ruby!"
What will be the output when you run ruby greet.rb in the terminal?
medium
A. Hello, Ruby!
B. puts "Hello, Ruby!"
C. SyntaxError
D. No output
Solution
Step 1: Understand the Ruby code
The code uses puts to print the string "Hello, Ruby!" to the terminal.
Step 2: Predict the output of running the script
Running ruby greet.rb executes the code and prints the string.
Final Answer:
Hello, Ruby! -> Option A
Quick Check:
puts prints string output = C [OK]
Hint: puts prints text exactly when script runs [OK]
Common Mistakes:
Thinking puts prints the code itself
Expecting no output from puts
Confusing syntax errors with correct code
4. You try to run a Ruby script with ruby myscript.rb but get an error: ruby: command not found. What is the most likely cause?
medium
A. The script file myscript.rb has a syntax error.
B. Ruby is not installed or not in your system's PATH.
C. You forgot to save the script file.
D. You need to add ./ before the filename.
Solution
Step 1: Analyze the error message
The error ruby: command not found means 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 B
Quick Check:
Command not found means Ruby missing or PATH issue [OK]
Hint: Command not found means Ruby missing or PATH issue [OK]
Common Mistakes:
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
5. You have two Ruby scripts in the same folder: 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?
hard
A. Use ruby run second.rb to specify the script.
B. Rename the files to have different extensions.
C. Always check your current folder and type the exact filename with extension.
D. Run scripts without typing the filename; Ruby will pick the right one.
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 C
Quick Check:
Exact filename avoids running wrong script [OK]
Hint: Double-check filename and folder before running script [OK]
Common Mistakes:
Thinking Ruby auto-selects the right script
Trying to run scripts without filenames
Using incorrect commands like 'ruby run'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.