Web Programming - Part 2: Ruby
# Web Programming
Yukihiro Matsumoto, "Matz"
# Optimized for developer happiness
# How to run Ruby
# The console
* Type commands one at a time * No mouse, use arrow keys * Hit Control+c if you get stuck * Control+a skips to beginning * Control+e skips to end
# Try it Type some math using +, -, *, / Use control keys and arrows to move around
# Time for some Ruby http://mislav.uniqpath.com/poignant-guide/
5.times { print "Odelay!" }
exit unless "restaurant".include? "aura"
['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
# Variables x y banana2 phone_a_quail
# Assignment teddy_bear_fee = 121.08
# Math Can use `+` `-` `*` `/` or `**` total = teddy_bear_fee + gratuity

Errors

NameError: undefined local variable or method `gratuity' for main:Object
    from (irb):3
    from /Users/matz/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.7/lib/rails/commands/console.rb:47:in `start'
    from /Users/matz/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.7/lib/rails/commands/console.rb:8:in `start'
    from /Users/matz/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.7/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
# Fix it gratuity = teddy_bear_fee * 0.20 total = teddy_bear_fee + gratuity
# Numbers 1 2 -10000 123.456
# Strings "Hello" "World"
# Symbols :a :name :id
# Methods often follow a dot! "hi matz".capitalize "".empty?
# Method arguments "mouse".pluralize(3)
# Arrays [1 ,2, 3] [3,4,2,4].sort ["a", 2, "c", :d]
# Changing Arrays
numbers = [1, 2]
numbers << 3
numbers.join(", ")
# Hashes data = { name: "Peter", profession: "lion tamer", great_love: "flannel" } puts data[:name]
# Changing hashes
data = {}
data[:name] = "John"
# Contidionals `nil` or `false` are "falsy" anything else is "truthy" plastic_cup = nil if plastic_cup print "Plastic cup is on the up 'n' up!" end

if/else

print( if at_hotel.nil?
         "No clue if he's in the hotel."
       elsif at_hotel == true
         "Definitely in."
       elsif at_hotel == false
         "He's out."
       else
         "The system is on the fritz."
       end )
# Blocks kitty_toys = [ {shape: 'sock', fabric: 'cashmere'}, {shape: 'mouse', fabric: 'calico'}, {shape: 'eggroll', fabric: 'chenille'} ] kitty_toys.sort_by { |toy| toy[:fabric] }
# Common blocks ['toast', 'cheese', 'wine'].each { |food| eat(food) }
# Methods def eat(food) "Eaten: " + food end
# Classes Group methods and variables class Person def initialize(name) @name = name end def say_hi puts "hi I'm " + @name end end
# Objects created from calling `new` on a class person1 = Person.new("bob") person1.say_hi

Inheritance

class Ninja < Person
  def swing_sword
    puts "Slash!"
  end
end

Some math homework

Write as the product of repeated factors. Then simplify.

0.53

def expand(n, e)
  parts = []
  e.times { parts << n }
  parts.join(" * ")
end
expand(0.5, 3)
eval expand(0.5, 3)
(−4)5 (2.7)2 23 (−5)6 (8.1)3
−43
-4**3
# Homework Think about what kind of website you want to build! What kind of classes or methods might go into your project? Example: A blog Posts: title, body, save, publish Comments: author, body