Preceding slideshow here
Go ahead and fire up a Ruby REPL. You can use irb
if you want...
Let's write the canonical first program, "Hello world!". In Ruby, it's very simple.
puts 'Hello world!'
Now, let's spice it up a bit. What's your favorite number? Let the world know!
puts 'Hello world!'
fav = 1 + 2 - 3 / 2 # what will the result be? use order of operations...
puts fav
Cool, and nerdy! Let's get some input...
puts "What's your favorite color?" # notice the dbl quotes
clr = gets.chomp # get input and remove trailing whitespace
puts clr.upcase
Ok, now let's do a quick group activity to introduce logic.
puts '---Enter in partner names---'
# if you are a leet programmer, find a faster/more interesting way to get the names
# perhaps use Arrays or Regexs if you like
puts 'First person?'
fst = gets.chomp
puts 'Second person?'
snd = gets.chomp
# now this is where it gets interesting
#let's define a method
# think of methods as doors, with arguments as keys
def ask_color_to (name)
puts "#{name}, what is your favorite color?"
return gets.chomp # the result/answer of running this method
end
fst_clr = ask_color_to(fst)
snd_clr = ask_color_to(snd)
if fst_clr == snd_clr
puts 'OMG BESTIES'
else
puts 'This is awkward...'
end
Let's talk about bigger things, namely Arrays and Hashes.
Arrays are lists of things, like [1,2,3]
or ['orange', 'custard', 'chocolate', 'chip']
.
Arrays in Ruby are accessed like this:
my_array = ['x', :y, 456, 789, false]
puts my_array
third = my_array[2] # INDEXING STARTS AT ZERO -> the first element is my_array[0]
new_third = third * 2
my_array[2] = new_third
puts my_array.size
puts my_array.methods # for a good time
# this is how you walk through arrays
my_array.each do |entry|
# put code here for what you want to do with each element of the array
puts entry
end
Hashes are lists that get "looked up" by Strings 'like me!'
or Symbols :or_me!
.
Think of them as key-value pairs: You look up values using the key!
They are called maps or dictionaries in other languages.
For example, if you wanted to share with your fellow UPL-ers data on what to get at restaurants in Madison:
food_data = {
qqs: ["General Tso's", 'Peanut Chicken'],
'ginger_root': ['Sesame Chicken', 'Potstickers'], # either Strings or Symbols are good for keys,
greenbush: 'DONUTS!!!',
oss: {
has_brats: true
}
}
puts food_data
puts food_data[:qqs]
puts food_data['qqs'] # what do you think will happen?
food_data['ginger root'].each do |food|
puts food.upcase
end
puts food_data[:oss][:has_brats]
food_data[:oss][:has_babaganoush] = false # making assumptions...
- Make a restaurant simulator where users enter in their order and your program reports the cost and decrements the inventory
- Make the above restaurant app with additional features, such as File I/O and OOP (classes)
- Make tic-tac-toe
- Make a calculator program (w/ File I/O and batch calculations)
- Design a class that gets instantiated based off of a JSON file
- Make small Ruby programs that simulate the
cat
,ls
,date
, andwc
commands in Unix - Try Project Euler problems
- Brainstorm and help others!
What will happen with a mismatch string vs. symbol?