Ruby Basic Syntax

Essential Ruby syntax and program structure

Program Structure

# Ruby program example
puts "Hello World!" # output to console
print "Text" # output without newline
p "Debug" # debug output

Variables

age = 25 # local variable
name = "John" # string variable
price = 99.99 # float variable
is_active = true # boolean variable
@instance_var = "value" # instance variable
@@class_var = "value" # class variable
$global_var = "value" # global variable

Data Types

42 # Integer
3.14 # Float
"text" # String
'a' # String (single char)
:symbol # Symbol (immutable)
true / false # Boolean
nil # Null value

String Operations

name = "John" # create string
"Hello #{name}" # string interpolation
text.length # get length
text.upcase # convert to uppercase
text.downcase # convert to lowercase
text.reverse # reverse string

Comments

# single line comment
=begin
multi-line
comment
=end