This episode covers Ruby's basic syntax: variables and constants with their naming rules, the Integer, Float, String, Symbol, Array, Hash, and Range data types, comments, output with p and puts, string interpolation, and the difference between symbols and strings.

Now you start touching the real thing: Ruby's basic syntax and data types. This material is the vocabulary of the Ruby language — variables, constants, numbers, strings, symbols, arrays, hashes, and ranges. All the following episodes are built on this understanding, so make sure every concept is truly mastered before moving on.
One of Ruby's appeals is syntax that feels like natural language. You won't find mandatory semicolons or braces for blocks. Instead, there are naming rules that carry meaning (prefixes like @ and $), and several ways to print output, each with a different role.
This episode 3 dissects variables and constants along with their rules, all the basic data types, comments and the output functions p/puts/print, as well as a deep comparison between symbols and strings — two concepts that often confuse beginners.
Ruby recognizes four variable scopes, distinguished by their naming prefixes:
nama = "Ruby" # local
@nama_instans = "Ruby" # instance (per object)
@@jumlah_total = 0 # class (shared across the whole class)
$versi_global = "4.0" # global (shared across the whole program)Local variables (nama) only live in the scope where they are defined. Instance variables (@nama_instans) belong to a single object and will be discussed in detail in episode 6. Class variables (@@jumlah_total) are shared by all objects of one class, while global variables ($versi_global) can be accessed from anywhere — and are therefore often avoided because they are prone to conflicts.
Constants begin with a capital letter and are written in the NAMA_BESAR form. Ruby will warn if a constant's value is changed:
BAHASA = "Ruby"
puts BAHASAThe practical difference: try changing BAHASA to a new value — Ruby emits a warning, not an error, because constants in Ruby can actually be changed even though it's not recommended. Method names ending in a question mark (like nil?) return a boolean, and names with an exclamation mark (like gsub!) indicate methods that mutate the original object. These conventions matter for reading Ruby code quickly.
Ruby provides Integer for whole numbers, Float for decimals, and String for text. Note that in Ruby, integers and decimals are distinct types with distinct methods:
umur = 30
harga = 19.99
puts umur.class
puts harga.classumur.class shows Integer and harga.class shows Float. Integer division is rounded down (for example 7 / 2 gives 3), so use floats if you want a decimal result. Strings are created with single or double quotes — the difference will be seen in the interpolation section.
Besides numbers and strings, there are four collection types you'll use very often:
:nama) — a lightweight string whose identity is compared, not its content.[1, 2, 3]) — an ordered, index-based collection.{ kunci: "nilai" }) — a pair of keys and values.1..10) — a range of values with an inclusive upper bound.simbol = :nama
daftar = [1, 2, 3]
hash = { bahasa: "Ruby", tahun: 1995 }
rentang = 1..10
puts hash[:bahasa]
puts rentang.cover?(5)The expression hash[:bahasa] retrieves the value for the key :bahasa, and rentang.cover?(5) checks whether 5 is within the range. Details of collection operations are covered thoroughly in episode 10.
Ruby also has three special values: true, false, and nil — which represents the absence of a value. All of them are objects, and only nil and false are considered falsy in a boolean context. Any other value like 0, "", or an empty array remains truthy. This rule differs from some other languages, so remember it well: only nil and false count as false.
Ruby has three ways to print output with different behaviors. puts adds a newline, print does not, and p shows the internal representation of a value — very useful for debugging:
ruby -e 'puts "baris pertama"; p [1, 2, 3]'The ruby -e command executes a single line of script without creating a file. In daily practice, use puts for normal output and p for inspecting values. Comments in Ruby are written with # and ignored by the interpreter:
# ini komentar satu baris
puts "Halo" # komentar di akhir barisComments don't affect execution, but they are very important for documentation. As a note, this is where the example code of previous episodes uses puts to display results to the terminal.
String interpolation is the way to insert the result of an expression into a string. This feature only works in double-quoted strings or strings using %Q:
bahasa = "Ruby"
puts "Saya belajar #{bahasa} versi #{RUBY_VERSION}"
puts "Hasil penjumlahan: #{2 + 3}"A single-quoted string like '#{bahasa}' will print the literal #{bahasa} without interpolation. puts "...#{bahasa}..." demonstrates how interpolation works, evaluating the expression inside the double curly braces.
Symbols and strings look similar but are used for different purposes. A symbol (:nama) is an identifier compared by identity — there is no String#gsub on symbols, and the same symbol always represents the same object throughout the program. A string ("nama") is data that can be changed.
puts :nama.equal?(:nama)
puts "nama".equal?("nama")The result is true for symbols (the same object) and false for strings (two different objects). The expression :nama.equal?(:nama) uses the equal? method, which compares object identity. That's why symbols are ideal for hash keys, flags, and method names — lightweight and fast to compare.
Tip
An important reading habit: when you see code like hash[:kunci], remember that :kunci is a symbol, not a string. Consistency between symbol and string keys in a hash is often a source of confusing bugs.
Episode 3 introduces Ruby's basic vocabulary: four variable types with prefix-based naming rules, constants, the Integer, Float, String, Symbol, Array, Hash, and Range data types, the truthy rule that only considers nil and false as false, three ways of producing output, and the important difference between symbols and strings.
Key takeaways:
@, class with @@, global with $.nil and false are falsy; every other value is truthy, including zero and empty strings.puts adds a newline, p shows the internal representation for debugging.#{} syntax.In the next episode, episode 4, we will discuss control flow and operators — if, unless, ternary, and case branching, while, until, loop loops and the each iterator, the short-circuit && and || operators, and the spaceship operator <=> which is unique to Ruby. The basic syntax you've mastered will start shaping program logic.