Hello World and Ruby Syntax
Every Ruby program starts somewhere, and the traditional first program is simply printing text to the screen. Beyond that trivial example, understanding Ruby's syntax conventions early pays off: how statements are terminated, how blocks are delimited, how method calls look with and without parentheses, and how files are structured. Ruby's syntax is notably permissive compared to many languages, which is part of what makes it feel conversational, but that same permissiveness means style conventions matter more for keeping code readable.
Cricket analogy: Just as a young cricketer starts with the basics of stance and grip before facing a fast bowler, every Ruby programmer starts with printing 'Hello, World' before learning the syntax conventions that make longer innings of code readable.
Your First Program
A Ruby program is simply a .rb file containing Ruby code, executed by the ruby interpreter. There is no required main function or class wrapper; top-level code executes sequentially from the first line. The puts method prints its argument followed by a newline, while print omits the trailing newline and p prints the argument's inspect representation, useful for debugging because it clearly shows strings, nil, and special characters.
Cricket analogy: A cricket match doesn't require a formal 'main ceremony' before the first ball is bowled — play simply starts; similarly a Ruby .rb file has no required main function, and puts prints a line the way a commentator announces the score, while p shows the raw value for debugging.
# hello.rb
puts "Hello, World!"
# puts vs print vs p
puts "line with newline" # adds \n automatically
print "no newline here" # no trailing newline
p "debug view" # => "debug view" (shows quotes)
p nil # => nil (visible, unlike puts nil which prints blank)
# Run with: ruby hello.rbCore Syntax Conventions
Ruby statements are typically one per line and do not require a terminating semicolon, though semicolons can separate multiple statements on one line. Blocks of code—method bodies, class definitions, conditionals, loops—are closed with the keyword end rather than braces, except for single-line blocks which use { }. Method calls often omit parentheses when unambiguous (puts "hi" is equivalent to puts("hi")), a stylistic choice embraced throughout idiomatic Ruby, though parentheses are recommended when passing multiple arguments or nesting calls for clarity.
Cricket analogy: A scorer doesn't need a comma after every entry on the scoresheet unless recording two events on one line; likewise Ruby statements need no semicolon per line, and just as an over is closed by six legal balls, a Ruby block is closed by end rather than braces.
Unlike Python, which uses significant whitespace/indentation to define blocks, Ruby uses explicit end keywords, so indentation is purely a style convention (typically two spaces) rather than something the interpreter enforces.
Omitting parentheses can create ambiguity in nested method calls, e.g. puts foo bar is often misread; Ruby style guides recommend adding parentheses once you pass arguments to a method that itself takes arguments.
- Ruby files use the
.rbextension and run top-level code without needing a main function. putsadds a trailing newline,printdoes not, andpshows theinspect(debug) representation.- Ruby blocks close with
end, not curly braces, except for single-line block syntax{ }. - Semicolons are optional; one statement per line is idiomatic.
- Parentheses on method calls are usually optional but recommended for clarity with multiple arguments.
- Indentation (typically 2 spaces) is a style convention, not syntactically required by the interpreter.
Practice what you learned
1. What is the standard file extension for Ruby source files?
2. What is the key difference between `puts` and `print` in Ruby?
3. How does Ruby typically close a method, class, or loop body?
4. What does the `p` method do that `puts` does not?
5. Are parentheses required on Ruby method calls?
Was this page helpful?
You May Also Like
What Is Ruby?
An introduction to Ruby's history, design philosophy, and the traits that make it a productive, expressive, object-oriented programming language.
Installing Ruby and Using IRB
How to install Ruby with a version manager, verify your setup, and use IRB (Interactive Ruby) to experiment with code in real time.
Variables and Data Types
Learn how Ruby variables work, its dynamic typing model, and the core built-in data types: integers, floats, strings, symbols, booleans, and nil.
Ruby Comments and Documentation
How to write single-line and multi-line comments in Ruby, and how to document code using RDoc/YARD conventions for generated API docs.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics