code for chapter 5 in ruby

This commit is contained in:
Leon Rische
2016-03-03 15:43:19 +01:00
parent 33d432bdaf
commit 4794bad93a
2 changed files with 24 additions and 0 deletions

View 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

View 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")