0
0
Rubyprogramming~5 mins

Running scripts with ruby command - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
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
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>
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
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
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.