Skip to content

Instantly share code, notes, and snippets.

@jarsen
Last active December 19, 2015 20:59
Show Gist options
  • Save jarsen/6017225 to your computer and use it in GitHub Desktop.
Save jarsen/6017225 to your computer and use it in GitHub Desktop.
# 1) open irb
# 2) `load "code_knight.rb"`
# 3) `knights = Marshal::load(Marshal::dump($knights))`
# 4) do the problems using your variable `knights`
# armor types - :light, :medium, :heavy
# ethical_alignment - :chaotic, :neutral, :lawful
# moral_alignment - :good, :neutral, :evil
class CodeKnight
attr_accessor :name, :health, :preferred_weapon, :armor_type, :ethical_alignment, :moral_alignment
def initialize(options)
@name = options[:name]
@health = options[:health]
@preferred_weapon = options[:preferred_weapon]
@armor_type = options[:armor_type]
@ethical_alignment = options[:ethical_alignment]
@moral_alignment = options[:moral_alignment]
@ethical_alignment = options[:ethical_alignment]
end
end
$knights = []
$knights << CodeKnight.new(name: "Jason", health: 50, preferred_weapon: :sword, armor_type: :medium, moral_alignment: :good, ethical_alignment: :chaotic)
$knights << CodeKnight.new(name: "Lancelot", health: 60, preferred_weapon: :lance, armor_type: :heavy, moral_alignment: :good, ethical_alignment: :lawful)
$knights << CodeKnight.new(name: "Gawain", health: 40, preferred_weapon: :sword, armor_type: :light, moral_alignment: :good, ethical_alignment: :neutral)
$knights << CodeKnight.new(name: "Death Knight", health: 48, preferred_weapon: :claymore, armor_type: :heavy, moral_alignment: :evil, ethical_alignment: :chaotic)
$knights << CodeKnight.new(name: "Zenn", health: 48, preferred_weapon: :mac, armor_type: :heavy, moral_alignment: :good, ethical_alignment: :lawful)
$knights << CodeKnight.new(name: "Dan", health: 50, preferred_weapon: :sword, armor_type: :light, moral_alignment: :evil, ethical_alignment: :lawful)
$knights << CodeKnight.new(name: "Sterling ", health: 800, preferred_weapon: :sword, armor_type: :light, moral_alignment: :evil, ethical_alignment: :chaotic)
$knights << CodeKnight.new(name: "Dawg", health: 50, preferred_weapon: :bowcaster, armor_type: :medium, moral_alignment: :neutral, ethical_alignment: :evil)
$knights << CodeKnight.new(name: "Rachelle", health: 50.5, preferred_weapon: :sword, armor_type: :light, moral_alignment: :lawful, ethical_alignment: :evil)
# Problem 0 (Freebie problem)
# Use Enumerable.each to print out a string for each knight in the format of "Jason prefers a sword"
# knights.each {|knight| puts "#{knight.name} prefers a #{knight.preferred_weapon}"}
# Problem 1
# Use Enumerable.find_all to find all knights with 50 or more health
# Problem 2
# Use Enumerable.find_all to find anyone who does not wear heavy armor
# Problem 3
# Use Enumerable.find_all to find all Knights who are chaotic.
# Problem 4
# Use Enumerable.find_all to find all Knights who are both chaotic and evil
# Problem 5
# Use Enumerable.map to create an array of the names of all the Knights
# Problem 6
# Use the solution from the last problem and Array.sort to sort your list of names in alphabetical order
# Problem 7
# use Enumerable.map and afterwards Array.uniq to find a list of all the weapons used by the knights
# Problem 9
# Use Enumerable.reduce to get a total of all the Knights' health
# Problem 10
# Use Enumerable.reduce to create a histogram of the weapons
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment