Programming Fundamentals

say "Welcome to Programming Fundamentals"

Day 8 (Wednesday)

Building a Menu

  # let's make a menu
  while true
    choice = ask "What would you like to do? (play, quit)"
    case choice.chomp
      when "play" then play  # play is a function we wrote, elsewhere
      when "quit" then break # break gets us out of the loop
    end
  end

Object Inheritence

class Bird  
  def preen  
    puts "I am cleaning my feathers."  
  end  
  def fly  
    puts "I am flying."  
  end  
end  
  
class Penguin < Bird  # A penguin is a bird.
  def fly  
    puts "Sorry. I'd rather swim."  
  end  
end  
  
p = Penguin.new  
p.preen  
p.fly

Day 7 (Tuesday)

Web.popup()

  Web.popup() do
    title "Hello World"             # big thing
    para "This is a paragraph!"     # smaller
  end

Insert a Picture

  p = Picture.new("Google.png")
  puts p

Hash, key-value pairs

    h = Hash.new("Go Fish")
    h["a"] = 100
    h["b"] = 200
    h["a"]           #=> 100
    h["c"]           #=> "Go Fish"
    # The following alters the single default object
    h["c"].upcase!   #=> "GO FISH"
    h["d"]           #=> "GO FISH"
    h.keys           #=> ["a", "b"]

Day 6 (Monday)

x.times loop

  4.times do
    say "hi"
    sleep 1
  end

Ranges

  x = (1..3)     # is a range, representing the numbers 1 through 3.
  y = ('a'..'z') # is a range, representing a lowercase alphabet.
  
  x.each do |x|                                                        
    puts x                                                               
  end
  
  y.each do |y|
    puts y
  end

More on nil, false, if, and unless

  cup = nil
  plastic = true
  if cup
    puts "The cup has stuff in it."
  end
  unless cup
    puts "The cup is empty."
  end
  
  cup = true
  
  puts "This is a plastic cup" if cup if plastic
  puts "This isn't a plastic cup" if cup unless plastic
  puts "This isn't a cup!" unless cup

Day 5 (Friday)

Loops


  # this will run forever
  while true
    say "hello"
  end

  # weird...
  unless true # opposite of while
    # does this even run?
  end

  # .upto method on numbers
  1.upto(6) do |x|
    say "Holding up #{x} fingers."
    sleep x
  end
  
  6.downto(1) do |x|
    say "Holding up #{x} fingers."
    sleep x
  end

Day 4 (Thursday)

Type Conversion

"1".to_i
# => 1

"1".to_f
# => 1.0

1.to_s
# => "1"

1.0.to_s
# => "1"

More List Stuff

We talked about types. We can do things to numbers. We can do things to strings. What can we do to lists?

[1, 2, 9001].max
# => 9001

ticket = [26, 73, 44, 21]

.sort

.sort! # consequences!

Day 3 (Wednesday)

Going Further: Sale's Tax

Types

rate = case state
   when "NY" then 0.075
   when "CT" then 0.060
end

Days 1 and 2 (Monday and Tuesday)

Playing with the Interactive Console

  4 + 2
  # 6
  # 4 and 2 are of the same type - they're numbers
  
  "Hello" + "World"
  # "HelloWorld"
  # strings are words in quotes. Always put quotes around words!
  
  "4" + "2"
  # "42"
  # "4" and "2" are of the same type - they're strings
  # when you add strings, they're slapped together
  
  4 + "2"
  # error - not the same type
  
  5 / 2
  # 2!!!
  # here's why: 5 and 2 are integers, and the answer of an operation with
  # integers is going to be an integer, too. To get a real answer:
  
  5.0 / 2.0
  # 2.5
  # ah, that's better.
  
  "Hello World".reverse
  # "dlroW olleH"
  # reverse is a _method_ that only works on Strings (words in quotes)
  
  "Hello World".length
  # 11
  # .length works on a string, but gives us back a number
  
  # Other "string" methods: .upcase, .downcase, .capitalize
  # What do you think they do?

My First Program

  # My first program.
  say "I'd like to get to know you."
  name = ask "What is your name?"
  say "Well, " + name + ", it's a pleasure to meet you."
  sleep 5   # pauses the program for five seconds

Conditional

if name == "Ricky"
  say "What a great name!"
end

or, use an "else if" - elsif

if name == "Ricky" say "What a great name!" elsif say "You have a pretty good name, but not as good as Ricky's." end

Resources