Running scripts with ruby command - Time & Space Complexity
When we run Ruby scripts using the ruby command, it's helpful to know how the time it takes grows as the script processes more data.
We want to understand how the script's running time changes when the input size changes.
Analyze the time complexity of the following code snippet.
def print_numbers(n)
i = 0
while i < n
puts i
i += 1
end
end
print_numbers(5)
This code prints numbers from 0 up to n-1 using a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that prints numbers.
- How many times: It runs once for each number from 0 to n-1, so n times.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times printing |
| 100 | 100 times printing |
| 1000 | 1000 times printing |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time to run the script grows in a straight line with the input size.
[X] Wrong: "The script runs instantly no matter how big n is."
[OK] Correct: Each number must be printed one by one, so more numbers mean more time.
Understanding how loops affect running time helps you explain your code clearly and shows you can think about efficiency.
"What if we changed the loop to print only even numbers up to n? How would the time complexity change?"
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
