Programming Fundamentals

say "Welcome to Programming Fundamentals"

Day 7 (Wednesday)

  # Write in my journal
  Web.popup('MyJournal') do
    title("Add a journal entry")
    editline("Title")
    editbox("Entry")
    buttons("cancel", "save")
  end

  # Read my Journal
  journal = Table('MyJournal').recent(10)
  puts journal

Day 6 (Tuesday)

Day 5 (Monday)

Advanced Dog

We're going to build a Dog that you can interact with. It's a little tricky at first, but it isn't too hard to follow.

class Dog
  @name
  @food
  
  def initialize(name, food)
    @name = name
    @food = food
  end
  
  def feed
    @food = @food + 1
  end
  
  def rollover
    if @food <= 3
      puts "I'm hungry. Feed me! :("
      return
    end
    puts "Rolling, rolling, rolling."
    @food = @food - 3
  end
  
  def status
    puts "I have #{@food} food."
  end
  
  def play
    while true
      print "What do you want to do? (feed, rollover, status): "
      ans = gets
      
      case ans.chomp
        when "feed" then self.feed
        when "rollover" then self.rollover
        when "status" then self.status
        else puts "I didn't get that. Try again?"
      end
    end
  end

end

# Now, play with the dog.
dog = Dog.new("Fido", 10)
dog.play
# Now what will happen?

Loops


  # 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

  # 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

Web.popup()

  Web.popup() do # check this...
    title "Hello World"
    para "This is a paragraph!"
  end

Lists (or, Arrays!)

class = ["John", "Jill", "Jack", "Janet"]
say class

Day 4 (Friday)

Class: Dog

# Let's make a dog.

# This is a "class definition".
# It's the blueprint for a Dog.
# We'll make a real dog later.
class Dog
  @name
  @owner
  @foodlevel

  # initialize is automatically called when you make a New Dog
  def initialize
    @foodlevel = 10
  end

  def feedme
    @foodlevel = @foodlevel + 1
  end

  def timegoesby
    @foodlevel = @foodlevel - 1
  end

  def setfoodlevel(aFoodlevel)
    @foodlevel = aFoodlevel
  end

  def setname(aName)
    @name = aName
  end

  def setowner(aOwner)
    @owner = aOwner
  end

  def bark
    say "bark!"
  end

  def sayhi
    say "I'm a dog. My name is #{@name}. My food level is #{@foodlevel}."
  end
end

# Now, let's make a new dog and play with it.
dog = Dog.new
dog.setname("Jon")
dog.sayhi
sleep 4

Day 3 (Thursday)

Sales Tax Calculator

# Sales Tax Calculator

answer = ask "How much are you spending?"
subtotal = answer.to_f # .to_f turns it into a number we can use!
rate = 0.075
tax = subtotal * rate
say "You'll pay #{subtotal} in sales tax, with a total of $#{subtotal+tax}."

Case Statement

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

Day 2 (Wednesday)

Let's review what we learned yesterday. Strings. Numbers.

Let's pick up where we left off with conditionals.

sleep 5     # pauses for five seconds

Day 1 (Tuesday)

Computer Science is the design and study of solving problems by creating procedures machines can perform. We'll be studying this field with a focus on exploring concepts and solving problems.

Hackety Hack

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."

Playing with the Interactive Ruby Shell

4 + 2
# 6

"4" + "2"
# "42"

4 + "2"
# error

"Hello World".reverse
# "dlroW olleH"

"Hello World".length
# 11

# Other "string" methods: .upcase, .downcase, .capitalize

Coming Soon

2.upto(5) do |x|
  say x
  sleep 1
end

# Everything is a class. Stings have methods, and numbers have methods!
# Try downcase on a number, or upto on a string.
# Let's write a word count program. How do we count words?
# What else can we do with this? Vowell counting?