Ruby Syntax
Ruby syntax is the set of rules for writing Ruby code. One of these rules covers the case of names.
One statement per line
Each statement sits on its own line. No statement ends with a semicolon.
puts "First line"
puts "Second line"No parentheses
The puts statement takes its text without parentheses. This course leaves the parentheses out.
puts "Plain and simple"An unterminated string
A string is text inside quotation marks. This string has no closing mark, so Ruby stops and reports an error.
puts "HelloExample
puts "One statement per line"
puts "No semicolons here"
puts "No parentheses here"Key points
- Syntax is the rule set that shapes each Ruby statement.
- Write one statement on each line.
- A statement needs no semicolon at its end.
- Indented lines use two spaces.
- Names are case sensitive.
- A change in case makes a new name.
- Parentheses are optional in Ruby.
- This course writes puts without parentheses.
In this example, three puts statements follow the syntax rules. Each statement sits on its own line. No statement ends with a semicolon. No statement uses parentheses.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz Ruby Compiler
Exercise
How many statements should sit on one line in this course?