Topics Covered: creating, updating, iterating over arrays (lists); same for hashes (dictionaries); symbols; basic methods; options hash for methods
Write a program that prints out the array ["apples", "bananas", "carrots", 4]
in reverse. Don't just flip the array in the code.
$ ruby reverse.rb
4
carrots
bananas
apples
Reduce tweet_analyzer.rb
as much as you can. Don't sacrifice clarity, however. How clear and concise can you make it? Is a different algorithm better?
While we've primarily seen examples of hashes mapping strings to integers or vice versa, the values can actually be anything (like array, or even other hashes). Create a hash (in ingredients.rb
) that maps a dish name (i.e. "Hash browns") to a list of necessary ingredients (i.e. "Potatoes", "Butter", "...is there anything else?"). Add at least 3 dishes. Each dish should have at least 3 ingredients.
Create a new file recipes.rb
. This time, map each dish name to another hash. That hash should have keys :description
(which maps to a String
), :ingredients
(which maps to an array of Strings, like above, and :steps
(which maps to an array of Strings that, you guessed it, tell you how to make the dish).
If you finish early, cook one of those dishes for your TA.
hash = {
:a => {
:b => ["c", "d"]
},
:e => "f"
}
When describing complex objects, it's nice to have a way to describe them. If I were to describe a hash that maps a symbol to a string, for example, I would write Hash<Symbol, String>
. For nested structures: Hash<Symbol, Array<String>>
(a hash that maps a symbol to an array of strings). Write down the signatures for:
ingredients.rb
In d2
, create a program that asks the user for items. Add each item to a list, and print out the whole list after each item.
$ ruby list_builder.rb
Welcome to list builder!
What can I add?
yummy things
Added! Your list is:
["yummy things"]
What can I add?
icky things
Added! Your list is:
["yummy things", "icky things"]
To keep a program running forever, put your code in a while true
loop. To exit such a program, press Ctrl-c
in the shell.
Create an extensive list builder, that allows you to add items with a command like add x
, remove items with remove x
, and quit with quit
$ ruby ext_list_builder.rb
Welcome to list builder++!
What can I do for you?
add a
Added! Your list is:
["a"]
What can I do for you?
add b
Added! Your list is:
["a", "b"]
remove a
Removed! Your list is:
["b"]
quit
Bye!
This is the pinnacle—it'll require knowledge from all the exercises up until this point! Since the program will be long, you may want to sketch out a solution before you start coding—remember what we said about planning. How do you keep track of whether you're done? What if the user makes a typo, tries a different command, or tries to remove something not in the list? Can they they see the list of available commands somehow?
After you come up with a solution, try to stress test, and check for edge cases (not typing anything, typing many words, etc.). Then ask one of the TA's to stress test for you and find any bugs!
Extend your program to use a Hash
instead of an Array
, and keep track of how many of each item the user has added.
$ ruby container.rb
Welcome to container builder!
What can I do for you?
add a
Added! Your container has:
{"a"=>1}
What can I do for you?
add b
Added! Your container has:
{"a"=>1, "b"=>1}
What can I do for you?
add a
Added! Your container has:
{"a"=>2, "b"=>1}
remove a
Removed! Your container has:
{"a"=>1, "b"=>1}
quit
Bye!
Translate the following code to use map
.
# map.rb
result = []
engines = ["Google", "Bing", "Ask Jeeves"]
engines.each do |e|
if e == "Google"
result.push("OK")
elsif e == "Bing"
result.push("Bad!")
else
result.push("What is that?")
end
end
result
# => ["OK", "Bad!", "What is that?"]
rubeque.com is a great site for simple Ruby puzzles. Try some out. To get you started:
split
and map
!)The final push! You'll be briefly presenting your website to the class tomorrow, so give it your all. This is what The Bru came up with, in less than 100 lines of HTML and 100 lines of CSS.