Skip to content

Instantly share code, notes, and snippets.

@andrewdavidcostello
Created March 7, 2012 21:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewdavidcostello/1996238 to your computer and use it in GitHub Desktop.
Save andrewdavidcostello/1996238 to your computer and use it in GitHub Desktop.
SaaS-Class: Homework 1
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 history getter
class_eval %{
def #{attr_name}=(val)
@#{attr_name} = val
@#{attr_name}_history = [nil] if @#{attr_name}_history.nil?
@#{attr_name}_history.push(val)
end
}
end
end
class Foo
attr_accessor_with_history :bar
end
f = Foo.new
f.bar = 1
f.bar = 2
f = Foo.new
f.bar = 4
p f.bar_history
class Anagram
def self.combine(words)
anagrams = words.uniq.inject Hash.new [] do |anagrams, word|
group = word.chars.sort.join
anagrams[group] += [word] if group == word.chars.sort.join
anagrams
end
anagrams.values #Output as array
end
end
p Anagram.combine(['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams', 'scream'])
class Dessert
attr_accessor :name
attr_accessor :calories
def initialize(name, calories)
@name = name
@calories = calories
end
def healthy?
@calories < 200
end
def delicious?
true
end
end
class JellyBean < Dessert
attr_accessor :flavor
def initialize(name, calories, flavor)
@name = name
@calories = calories
@flavor = flavor
end
def delicious?
@flavor == 'black licorice' ? false : true
end
end
dessert = Dessert.new('cheesecake',250)
p dessert.delicious?
p dessert.healthy?
bean = JellyBean.new('bertiebots',150,'cinnamon')
p bean.delicious?
p bean.healthy?
class String
def palindrome?
self.gsub!(/\W+/, '').downcase!
self == self.reverse
end
def word_count
downcase.split.inject Hash.new(0) do |count, word|
count[word] +=1
count
end
end
end
print "A man, a plan, a canal -- Panama".palindrome?
print "Doo bee doo bee doo".word_count
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
class RockPaperScissors
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
game.each do |player, strategy|
raise NoSuchStrategyError unless ['R','P','S'].include?(strategy.upcase)
end
if compare?(game)
puts game[0][0] + " Wins!"
game[0]
else
puts game[1][0] + " Wins!"
game[1]
end
end
def rps_tournament_winner(game)
if game[0][1].class==String
rps_game_winner(game)
else
branch1=rps_tournament_winner(game[0])
branch2=rps_tournament_winner(game[1])
rps_tournament_winner([branch1,branch2])
end
end
def compare?(game)
(game[0][1] + game[1][1]) =~ /rs|sp|pr|rr|ss|pp/i
end
end
game = RockPaperScissors.new
list = [ [ "Armando", "P" ], [ "Dave", "S" ] ]
game.rps_game_winner(list)
tournament = [ [
[ [ "Armando", "P" ], [ "Dave", "S" ] ],
[ [ "Richard", "R"], ["Michael", "S"] ],
],
[
[ ["Allen", "S"], ["Omer", "P"] ],
[ ["David E.", "R"], ["Richard X.", "P"] ]
] ]
game.rps_tournament_winner(tournament)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment