commit
b8e75add4e
@ -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
|
@ -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
|
@ -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
|
@ -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
|
Loading…
Reference in new issue