Your first Ruby script
Hello file.rb
A Ruby script is a plain text file with the rb extension. Open any text editor, type the line below, and save it as hello.rb in your working folder.
puts "Hello, Ruby"
That single line is a complete program. The next section runs it and shows its output.
ruby file.rb
Run the file by passing its name to the ruby command. The interpreter reads hello.rb from top to bottom and prints the result:
Run the script
ruby hello.rb
Hello, Ruby
The terminal prints Hello, Ruby and nothing else. No server and no browser are needed for plain scripts like this one.
puts prints
puts prints text to the terminal and moves to a new line afterwards, so each call starts on a fresh line. Its sibling print writes without that final newline.
puts "one" puts "two"
one
two
Common beginner mistakes
Three mistakes catch almost every beginner. First, typing only the file name runs nothing: the ruby command must come first, as in ruby hello.rb.
Second, a typo such as put instead of puts calls a method that does not exist. Third, an unclosed quote swallows the rest of the line, so close each string on the same line you open it:
puts "Quotes come in pairs: open and close on one line"
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which extension does a Ruby script file use?
Answer
.rb
hello.rb is a Ruby script; the interpreter does not require the extension, but everything names .rb.
-
What does this program print?
puts "Hello, Ruby"Answer
Hello, Ruby
puts prints the text of the string without its quotes and ends the line.
-
Which command runs the saved file hello.rb?
Answer
ruby hello.rb
The file name alone is not a command; the interpreter comes first and takes the file as its argument.
-
What does this program print?
puts "one" puts "two"Answer
one / two
Every puts ends its line, so the second call starts on a fresh one.
-
How does print differ from puts?
Answer
print writes no newline at the end
print keeps the next output on the same line; puts ends the line.
-
What does the shell answer to the bare line
hello.rbfor a file that is not executable?Answer
command not found
The shell looks for a program of that name on PATH; the ruby command is what runs the script.
-
You wrote
put "hi"instead ofputs "hi". What happens?Answer
Nothing is printed, and Ruby reports an undefined method
putRuby raises a NoMethodError and suggests puts among the near names.
-
What does an unclosed quote on a line cause?
Answer
A syntax error: unterminated string meets end of file
The parser reaches the end of the file while the string is still open, and reports it.
-
What does this program print?
name = "Ada" print "Hello, " puts nameAnswer
Hello, Ada
print leaves the cursor after the comma, and puts writes the name and its newline on the same line.
-
What does a plain Ruby script need besides the interpreter?
Answer
Nothing else: the text file and ruby are enough
A script prints to the terminal with no server and no browser.
Article author: Arthur Isaev