Ruby Language Overview handout #7
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", "joe"=>"biden", "donald"=>"trump"}
class Foo [< ]
end
def f(p1, p2, ..., pn)
end
def f(p1 = v1, p2 = v2, ..., pn = vn)
end
Syntactic sugar for defining getters and setters:
attr_reader symbol1, symbol2, ..., symbol-n
attr_writer symbol1, symbol2, ..., symbol-n
The constructor for a class always calls a method called initialize.
public/protected/private keywords used inside a class to mean "starting
here, constructs have this kind of access"
instance-variables start with @
class-variables (static variables) start with @@
symbols start with :
if [then]
elsif [then]
else
end
while [do]
end
for in [do]
end
loop do
break if
end
.() { }
.() do
end
can begin with |params| and has a sequence of statements
embed an expression to be evaluated in a string:
"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: Math.sqrt, Math.sin, Math.cos
relational: <, >, <=, >=, !=, ==
logical: &&, ||, !
conversion: .to_f, .to_i, .round, Stuart Reges
Last modified: Mon Feb 24 09:07:26 PST 2025