Skip to content
This repository was archived by the owner on May 23, 2020. It is now read-only.

Commit 623dc2c

Browse files
hashing example added
1 parent 8f72d93 commit 623dc2c

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ print 'Good work!!' unless jobNotDone
131131
Complex expressions can be created using these operators: E.g. *(x && (y || w)) && z*</br>
132132
Expressions in parentheses are always evaluated before anything outside parentheses.
133133

134+
__case__ is an alternative to _if_ _else_ statements.
135+
```ruby
136+
case language
137+
when "JS"
138+
puts "Websites!"
139+
when "Ruby"
140+
puts "Web apps!"
141+
else
142+
puts "I don't know!"
143+
end
144+
```
145+
134146
## Loops and Iterators
135147
Use of while:
136148
```ruby
@@ -361,4 +373,4 @@ my_hash.each_key { |k| print k, " " }
361373
362374
my_hash.each_value { |v| print v, " " }
363375
# ==> 1 2 3
364-
```
376+
```

movie_ratings_update.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
movies = {
2+
Memento: 3,
3+
Primer: 4,
4+
Ishtar: 1
5+
}
6+
7+
puts "What would you like to do?"
8+
puts "-- Type 'add' to add a movie."
9+
puts "-- Type 'update' to update a movie."
10+
puts "-- Type 'display' to display all movies."
11+
puts "-- Type 'delete' to delete a movie."
12+
13+
choice = gets.chomp.downcase
14+
case choice
15+
when 'add'
16+
puts "What movie do you want to add?"
17+
title = gets.chomp
18+
if movies[title.to_sym].nil?
19+
puts "What's the rating? (Type a number 0 to 4.)"
20+
rating = gets.chomp
21+
movies[title.to_sym] = rating.to_i
22+
puts "#{title} has been added with a rating of #{rating}."
23+
else
24+
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
25+
end
26+
when 'update'
27+
puts "What movie do you want to update?"
28+
title = gets.chomp
29+
if movies[title.to_sym].nil?
30+
puts "Movie not found!"
31+
else
32+
puts "What's the new rating? (Type a number 0 to 4.)"
33+
rating = gets.chomp
34+
movies[title.to_sym] = rating.to_i
35+
puts "#{title} has been updated with new rating of #{rating}."
36+
end
37+
when 'display'
38+
movies.each do |movie, rating|
39+
puts "#{movie}: #{rating}"
40+
end
41+
when 'delete'
42+
puts "What movie do you want to delete?"
43+
title = gets.chomp
44+
if movies[title.to_sym].nil?
45+
puts "Movie not found!"
46+
else
47+
movies.delete(title.to_sym)
48+
puts "#{title} has been removed."
49+
end
50+
else
51+
puts "Sorry, I didn't understand you."
52+
end

0 commit comments

Comments
 (0)