Print Output
puts "Hello"Prints a line.
Ruby syntax quick reference: blocks, arrays, hashes, classes and exceptions, from the official Ruby docs. Every item below is clearly marked as a shortcut, command, or syntax reference.
puts "Hello"Prints a line.
items.each { |item| puts item }Loops over every item.
"Hi #{name}"Inserts values into a string.
name = "Ada"Assigns a value to a variable.
if x > 0
...
elsif x == 0
...
else
...
endRuns code depending on a condition.
nums = [1, 2, 3]
nums << 4Creates an ordered list.
user = { name: "Ada", age: 36 }Creates key-value pairs.
def greet(name = "friend")
"Hi #{name}"
endDefines a method.
Prints a line.
puts "Hello"Loops over every item.
items.each { |item| puts item }Inserts values into a string.
"Hi #{name}"Assigns a value to a variable.
name = "Ada"Runs code depending on a condition.
if x > 0
...
elsif x == 0
...
else
...
endCreates an ordered list.
nums = [1, 2, 3]
nums << 4Creates key-value pairs.
user = { name: "Ada", age: 36 }Defines a method.
def greet(name = "friend")
"Hi #{name}"
endTransforms each item.
nums.map { |n| n * 2 }Keeps items matching a condition.
nums.select(&:even?)Defines a class with an initializer.
class Dog
attr_reader :name
def initialize(name)
@name = name
end
endMatches a value against cases.
case n
when 1 then "one"
else "other"
endRepeats a block N times.
3.times { |i| puts i }Handles exceptions.
begin
...
rescue StandardError => e
puts e.message
endLoads a library or file.
require "json"Runs a Ruby file.
ruby script.rbRuns code when a condition is false.
puts "empty" unless list.any?A lightweight, immutable name.
:statusCalls a method only if the object isn't nil.
user&.nameReads a whole file as a string.
text = File.read("a.txt")It covers 20 common Ruby patterns and examples, including Print Output, Iterate with each, String Interpolation, Declare a Variable, If / Elsif / Else.
Yes. Each reference page includes a copyable code example.
Check the linked documentation for language or runtime version requirements. Libraries, file paths, terminal quoting and environment settings can differ across systems; no blanket platform compatibility is implied.