Files
grokking_algorithms/05_hash_tables/ruby/02_check_voter.rb
Timo J c849e75041 More concise Rubyish code.
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.
2019-12-08 12:03:15 +01:00

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