From b8e75add4e1628b4c5cfd68c52533ddbdb9f39fe Mon Sep 17 00:00:00 2001 From: alansnape Date: Fri, 4 Nov 2016 15:17:44 +0800 Subject: [PATCH] 2016-11-04 15:17:44 --- attr_accessor_with_history.rb | 28 ++++++++++++++++++++ dessert.rb | 30 +++++++++++++++++++++ fun_with_strings.rb | 31 ++++++++++++++++++++++ rock_paper_scissors.rb | 49 +++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 attr_accessor_with_history.rb create mode 100644 dessert.rb create mode 100644 fun_with_strings.rb create mode 100644 rock_paper_scissors.rb diff --git a/attr_accessor_with_history.rb b/attr_accessor_with_history.rb new file mode 100644 index 0000000..98e76ce --- /dev/null +++ b/attr_accessor_with_history.rb @@ -0,0 +1,28 @@ +# By alan snape +# All rights reserved. + +class Class + def attr_accessor_with_history(attr_name) + attr_name = attr_name.to_s # make sure it's a string + attr_reader attr_name # create the attribute's getter + attr_reader attr_name+"_history" # create bar_history getter + class_eval %Q| + @#{attr_name} = nil + @#{attr_name}_history = nil + def #{attr_name} + @#{attr_name} + end + def #{attr_name}=(value) + if @#{attr_name} then + @#{attr_name}_history.push(@#{attr_name}) + else + @#{attr_name}_history = [@#{attr_name}] + end + @#{attr_name} = value + end + def #{attr_name}_history + @#{attr_name}_history + end + | + end +end diff --git a/dessert.rb b/dessert.rb new file mode 100644 index 0000000..bc98ca8 --- /dev/null +++ b/dessert.rb @@ -0,0 +1,30 @@ +# By alan snape +# All rights reserved. + +class Dessert + attr_accessor :name, :calories + def initialize(name, calories) + self.name = name + self.calories = calories + end + def healthy? + return self.calories < 200 + end + def delicious? + return true + end +end + +class JellyBean < Dessert + attr_accessor :flavor + def initialize(flavor) + self.flavor = flavor + self.calories = 5 + end + def name + return self.flavor + ' jelly bean' + end + def delicious? + return 'licorice' != self.flavor + end +end diff --git a/fun_with_strings.rb b/fun_with_strings.rb new file mode 100644 index 0000000..3b3e3c9 --- /dev/null +++ b/fun_with_strings.rb @@ -0,0 +1,31 @@ +# By alan snape +# All rights reserved. + +module FunWithStrings + def palindrome? + str = self.downcase.gsub(/(\W|\d)/, '') + return str == str.reverse + end + def count_words + w = {} + self.downcase.split(/\W+/).each do |i| + w.has_key?(i) ? w[i] += 1 : w[i] = 1 + end + w.delete("") + return w + end + def anagram_groups + w = {} + self.split.each do |s| + i = s.downcase.chars.sort.join + w.has_key?(i) ? w[i].push(s) : w[i] = [s] + end + return w.values + end +end + +# make all the above functions available as instance methods on Strings: + +class String + include FunWithStrings +end diff --git a/rock_paper_scissors.rb b/rock_paper_scissors.rb new file mode 100644 index 0000000..5b0ade7 --- /dev/null +++ b/rock_paper_scissors.rb @@ -0,0 +1,49 @@ +# By alan snape +# All rights reserved. + +class RockPaperScissors + + # Exceptions this class can raise: + class NoSuchStrategyError < StandardError; end + + # check strategy is legal or not + def self.checkStrategy(stg) + if 'P' != stg && 'R' != stg && 'S' != stg then + raise NoSuchStrategyError, 'Strategy must be one of R,P,S' + end + end + + # a single round game + def self.winner(player1, player2) + stg1, stg2 = player1[1], player2[1] + self.checkStrategy(stg1) + self.checkStrategy(stg2) + if stg1 == stg2 then + return player1 + elsif 'P' == stg1 && 'S' == stg2 then + return player2 + elsif 'S' == stg1 && 'P' == stg2 then + return player1 + else + return stg1 < stg2 ? player1 : player2 + end + end + + # if there is a sub game then returns the winner of it + # else return this player himself + def self.tournament_match(game) + if game[0].kind_of?(Array) then + return self.tournament_winner(game) + else + return game + end + end + + # tournament game rounds + def self.tournament_winner(tournament) + player1 = self.tournament_match(tournament[0]) + player2 = self.tournament_match(tournament[1]) + return self.winner(player1, player2) + end + +end