100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Ruby

Hello World and Ruby Syntax

Write your first Ruby program and learn the fundamental syntax conventions: statements, indentation, method calls, and file structure.

Ruby FoundationsBeginner8 min readJul 9, 2026
Analogies

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.

ruby
# 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.rb

Core 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 .rb extension and run top-level code without needing a main function.
  • puts adds a trailing newline, print does not, and p shows the inspect (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

Was this page helpful?

Topics covered

#Ruby#RubyStudyNotes#Programming#HelloWorldAndRubySyntax#Hello#World#Syntax#Program#StudyNotes#SkillVeris