Ruby Language Overview handout #9 Summary of Ruby features numbers: Integer (3, 802), Float (3.4) booleans: true, false strings: "hello", "how are you?" arrays: [3, 15, "hello", [7, "howdy"], 308.4, true] ranges: 1..10, 'a'..'z' hash: {"stuart"=>"reges", "donald"=>"trump"} Hash.new(default value) enumerators: n.times, x.each class Foo [< <superclass>] <definitions> end def f(p1, p2, ..., pn) <body> end def f(p1 = v1, p2 = v2, ..., pn = vn) <body> end attr_reader symbol1, symbol2, ..., symbol-n attr_writer symbol1, symbol2, ..., symbol-n constructor is "initialize", self is implicit parameter, clone makes a copy public/protected/private keywords used inside a class mean "starting here" use prefix @ for instance variables, @@ for class variables, : for symbols if <bool-expr> [then] <body> elsif <bool-expr> [then] <body> else <body> end while <bool-expr> [do] <body> end for <variable> in <collection> [do] <body> end loop do <statements> break if <bool-expr> <statements> end <variable>.<method>(<params>) { <block> } <variable>.<method>(<params>) do <block> end <block> can begin with |params| and has a sequence of statements "text before #{expression to evaluate} text after" a method takes a block if it includes calls on yield nil is a special value that represents "no object" Useful methods arithmetic: +, -, *, /, %, ** (exponentiation), +=, -=, *= mathematical: n.abs, Math.[sqrt, sin, cos, log, log10] relational: <, >, <=, >=, !=, ==, <=> logical: &&, ||, !, and, or, not conversion: <Integer>.to_f, <Float>.[round, to_i], <Object>.to_s random: rand, rand(<integer>) higher-order: map, filter, find, find_all, any?, all?, inject, reduce Array/String: x.length # of elements (also Hash, size is a synonym) x[i] element i (0-based indexing, also Hash) x[i, n] slice with n elements starting at i x[i..j] slice with values at index i through j x << v append v to x x.index v index of v in x x.delete v delete all occurrences of v from x (also Hash) x.reverse values in reverse order x.sort sort values (Arrays only) push v appends v to end of array (Arrays only) pop remove and return top value (Arrays only) chomp eliminate trailing newline from string (String only) upcase/downcase uppercase/lowercase version of string (String only) split, split("") split on whitespace, split characters (String only) each_with_index yield value/index pairs (Arrays/Hash only) x.member? v does it contain v? (Arrays/Hash only) Input/Output puts x like Java println print x like Java print (stay on current output line) print x, y print multiple values gets reads a line of input File.open(s) returns a file object for given file (implements each) <file>.gets read a line from file object
Stuart Reges
Last modified: Fri Jun 5 11:29:07 PDT 2026