contentintech
Languages

Ruby

Enumerable, hashes and blocks for concise Ruby programs.

Values and collections

name = "Ada"Bind a string
names = ["Ada", "Lin"]Create an array
user = { name: "Ada", id: 1 }Create a symbol-keyed hash
user.fetch(:role, "reader")Read with an explicit fallback

Enumerable

[1, 2, 3].map { |n| n * 2 }Transform elements
[1, 2, 3].select { |n| n.odd? }Filter by a predicate
[1, 2, 3].sumSum numeric elements
names.each_with_index { |name, i| puts "#{i}: #{name}" }Iterate with positions

Methods and files

def add(a, b); a + b; endReturn the final expression of a method
File.read("notes.txt")Read a file into a string
File.foreach("notes.txt") { |line| puts line }Iterate through lines
ruby -c app.rbCheck source syntax

Section navigation