code for chapter 5 in ruby
This commit is contained in:
8
05_hash_tables/ruby/01_price_of_groceries.rb
Normal file
8
05_hash_tables/ruby/01_price_of_groceries.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
# {} is the same as Hash.new
|
||||
book = {}
|
||||
# an apple costs 67 cents
|
||||
book["apple"] = 0.67
|
||||
# milk costs $1.49
|
||||
book["milk"] = 1.49
|
||||
book["avocado"] = 1.49
|
||||
puts book
|
||||
16
05_hash_tables/ruby/02_check_voter.rb
Normal file
16
05_hash_tables/ruby/02_check_voter.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
# in order to be accessible from within the check_voter function
|
||||
# voted has to be an instance variable (or global / class)
|
||||
@voted = {}
|
||||
|
||||
def check_voter(name)
|
||||
if @voted.key?(name) && @voted[name]
|
||||
puts "kick them out!"
|
||||
else
|
||||
@voted[name] = true
|
||||
puts "let them vote!"
|
||||
end
|
||||
end
|
||||
|
||||
check_voter("tom")
|
||||
check_voter("mike")
|
||||
check_voter("mike")
|
||||
Reference in New Issue
Block a user