From 4794bad93af02030fa3710ba80c3b0783ce26611 Mon Sep 17 00:00:00 2001 From: Leon Rische Date: Thu, 3 Mar 2016 15:43:19 +0100 Subject: [PATCH] code for chapter 5 in ruby --- 05_hash_tables/ruby/01_price_of_groceries.rb | 8 ++++++++ 05_hash_tables/ruby/02_check_voter.rb | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 05_hash_tables/ruby/01_price_of_groceries.rb create mode 100644 05_hash_tables/ruby/02_check_voter.rb diff --git a/05_hash_tables/ruby/01_price_of_groceries.rb b/05_hash_tables/ruby/01_price_of_groceries.rb new file mode 100644 index 0000000..9763b25 --- /dev/null +++ b/05_hash_tables/ruby/01_price_of_groceries.rb @@ -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 diff --git a/05_hash_tables/ruby/02_check_voter.rb b/05_hash_tables/ruby/02_check_voter.rb new file mode 100644 index 0000000..0832e52 --- /dev/null +++ b/05_hash_tables/ruby/02_check_voter.rb @@ -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")