Bird
Raised Fist0
Rubyprogramming~10 mins

Running scripts with ruby command - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Running scripts with ruby command
Write Ruby script file
↓
Open terminal/command line
↓
Type: ruby filename.rb
↓
Ruby interpreter reads file
↓
Execute code line by line
↓
Output results to terminal
↓
Script ends
This flow shows how you write a Ruby script, run it using the ruby command in the terminal, and see the output.
Execution Sample
Ruby
puts "Hello, Ruby!"
puts 2 + 3
This script prints a greeting and the result of 2 plus 3.
Execution Table
StepActionCode ExecutedOutput
1Start script executionputs "Hello, Ruby!"Hello, Ruby!
2Execute next lineputs 2 + 35
3End of scriptNo more linesScript ends
💡 Reached end of file, script stops
Variable Tracker
VariableStartAfter Step 1After Step 2Final
No variables----
Key Moments - 3 Insights
Why do I need to type 'ruby filename.rb' in the terminal?
Typing 'ruby filename.rb' tells the computer to use the Ruby interpreter to read and run your script file, as shown in execution_table step 1.
What happens if I run 'ruby' without a filename?
Without a filename, Ruby opens an interactive prompt instead of running a script file, so no script lines execute as in execution_table.
Why does the output appear in the terminal?
The 'puts' command prints text to the terminal, so each executed line outputs immediately, as seen in execution_table steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 2?
A2 + 3
BHello, Ruby!
C5
DNo output
💡 Hint
Check the 'Output' column in row for step 2 in execution_table
At which step does the script finish running?
AStep 3
BStep 2
CStep 1
DNever finishes
💡 Hint
Look at the 'Action' column in execution_table where it says 'End of script'
If you add 'puts "Bye!"' after line 2, what will happen in the execution table?
AThe output '5' will change to 'Bye!'
BA new step will show output 'Bye!' after step 2
CThe script will stop before printing 'Bye!'
DNo change in output
💡 Hint
Adding a line adds a new step with its output, similar to steps 1 and 2
Concept Snapshot
Run Ruby scripts by typing 'ruby filename.rb' in terminal
Ruby reads and runs the file line by line
Output from 'puts' shows in terminal
Script ends when file ends
No special setup needed for simple scripts
Full Transcript
To run a Ruby script, first write your code in a file with .rb extension. Then open your terminal or command line. Type 'ruby' followed by your file name, like 'ruby script.rb'. The Ruby interpreter reads your file and runs each line in order. Commands like 'puts' print output to the terminal immediately. When the interpreter reaches the end of the file, the script stops running. This process lets you see your program's results right away.

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

  1. Step 1: Understand the purpose of the ruby command

    The ruby command is used to run Ruby scripts saved in files from the terminal.
  2. Step 2: Differentiate from other Ruby tools

    It does not compile code, open the console, or install gems automatically.
  3. Final Answer:

    It runs a Ruby script saved in a file. -> Option A
  4. 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

  1. Step 1: Identify the correct command syntax

    The correct syntax to run a Ruby script is ruby filename.rb.
  2. Step 2: Check each option

    Only ruby hello.rb matches the correct syntax.
  3. Final Answer:

    ruby hello.rb -> Option D
  4. 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

  1. Step 1: Understand the Ruby code

    The code uses puts to print the string "Hello, Ruby!" to the terminal.
  2. Step 2: Predict the output of running the script

    Running ruby greet.rb executes the code and prints the string.
  3. Final Answer:

    Hello, Ruby! -> Option A
  4. 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

  1. Step 1: Analyze the error message

    The error ruby: command not found means the terminal cannot find the ruby program.
  2. Step 2: Identify the cause

    This usually means Ruby is not installed or its location is not in the system PATH environment variable.
  3. Final Answer:

    Ruby is not installed or not in your system's PATH. -> Option B
  4. 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

  1. Step 1: Understand the cause of the mistake

    Typing the wrong filename runs the wrong script because the command requires exact filenames.
  2. 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.
  3. Final Answer:

    Always check your current folder and type the exact filename with extension. -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard