Strings and Numbers


to_s converts to strings
to_i converts to ints
to_a converts to arrays

Arrays store information in a sequence.

[].max returns object in array with maximum value

variable = [] assignments

variable.sort! ! means mutate the existing object

variable['access'] access property in object

? is a convention that returns a boolean

A Hash is a dictionary-like collection of unique keys and their values.

A block is a chuck of code surrounded by curly braces.
Blocks are always attached to methods. do end can be used when the block goes on several lines.

def method( arg ) to define a function method. Use end to terminate.

require lib to import libraries.


Use unless instead of if ! (if not). Can be used with else.

unless tweets.empty?
  puts tweets
  puts "empty"

if and unless can also be written inline:

fail "Password too short" if password.length > 8
fail "No username set" unless username

nil is falsey.

"" empty string is truthy
0 is truthy
[] empty array is truthy

tweets = timeline.tweets || []

variable ||= 2 will assign if not yet

Conditionals always return a value in Ruby. Can be used to return from method (will be returned by method)

options[:path] = if list_name

Case statements can be written with when/then on same line.