Hash key checking is not necessary in this case. If a key doesn't exist in a Hash, Ruby returns nil by default and nil is falsy. Hash key checking would only be necessary if the Hash had been set up with a default value.
17 lines
330 B
Ruby
17 lines
330 B
Ruby
# 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[name]
|
|
puts "kick them out!"
|
|
else
|
|
@voted[name] = true
|
|
puts "let them vote!"
|
|
end
|
|
end
|
|
|
|
check_voter("tom")
|
|
check_voter("mike")
|
|
check_voter("mike")
|