say "Welcome to Programming Fundamentals"
# 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
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
Web.popup() do title "Hello World" # big thing para "This is a paragraph!" # smaller end
p = Picture.new("Google.png") puts p
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"]
4.times do say "hi" sleep 1 end
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
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
# 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
"1".to_i # => 1 "1".to_f # => 1.0 1.to_s # => "1" 1.0.to_s # => "1"
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!
rate = case state when "NY" then 0.075 when "CT" then 0.060 end
Lists
class = ["John", "Jill", "Jack", "Janet"] say class # What happens?
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. 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
if name == "Ricky" say "What a great name!" endor, 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