Data Types & Interpolation
Ruby is a purely object-oriented language. Every data type, including primitive numbers and strings, is an object containing built-in methods.
1 String Interpolation & Symbols
Ruby provides unique data types:
- String Interpolation (`#{"#{expr}"}`): Double-quoted strings evaluate embedded code sequences wrapped in curly braces. Single-quoted strings do not perform evaluation and treat characters as literals.
- Symbols (`:my_symbol`): Immutable, reusable string-like identifiers. Unlike strings, only one instance of a symbol exists in memory, making them excellent for hash keys.
2 Type Interpolation Code
Let's run a program exploring double-quoted evaluations and Symbols comparison:
Ruby — Data Types
▶ Run Code
item = "book"
price = 14.99
# Double quotes process interpolation
puts "The #{item} costs $#{price}"
# Symbols demo
status_ok = :ok
status_err = :error
puts "Symbol type: #{status_ok.class}"
puts "Comparing symbol IDs: #{:ok.object_id == :ok.object_id}"
puts "Comparing string IDs: #{"ok".object_id == "ok".object_id}"
3 Code Challenge
Challenge: Declare a string in single quotes and another in double quotes, both containing a variable interpolation sequence. Print both strings to verify the difference.