Skip to content

Instantly share code, notes, and snippets.

@acook
Last active December 21, 2015 00:28
Show Gist options
  • Save acook/6220229 to your computer and use it in GitHub Desktop.
Save acook/6220229 to your computer and use it in GitHub Desktop.
class Game
def initialize
@player = Player.new
@gui = GUI.new(self)
end
attr :player, :gui
class Player
attr_accessor :hit_points, :max_hit_points
def health
(max_hit_points.to_f / hit_points.to_f * 100).to_i
end
end
class GUI
def initialize game
@game = game
end
attr :game
def display_health
puts game.player.health
end
end
end
g = Game.new
g.player.max_hit_points = 10
g.player.hit_points = 12
g.gui.display_health
$ pry
2.0.0 (Object#main):0 > class Game
2.0.0 0 *
2.0.0 0 * def initialize
2.0.0 0 * @player = Player.new
2.0.0 0 * @gui = GUI.new(self)
2.0.0 0 * end
2.0.0 0 * attr :player, :gui
2.0.0 0 *
2.0.0 0 * class Player
2.0.0 0 * attr_accessor :hit_points, :max_hit_points
2.0.0 0 *
2.0.0 0 * def health
2.0.0 0 * (max_hit_points.to_f / hit_points.to_f * 100).to_i
2.0.0 0 * end
2.0.0 0 * end
2.0.0 0 *
2.0.0 0 * class GUI
2.0.0 0 * def initialize game
2.0.0 0 * @game = game
2.0.0 0 * end
2.0.0 0 * attr :game
2.0.0 0 *
2.0.0 0 * def display_health
2.0.0 0 * puts "HEALTH: #{game.player.health}"
2.0.0 0 * end
2.0.0 0 * end
2.0.0 0 * end
=> nil
2.0.0 (Object#main):0 >
2.0.0 (Object#main):0 > g = Game.new
=> #<Game:0x007fa88a881de8
@gui=#<Game::GUI:0x007fa88a881d48 @game=#<Game:0x007fa88a881de8 ...>>,
@player=#<Game::Player:0x007fa88a881d98>>
2.0.0 (Object#main):0 > g.player.max_hit_points = 10
=> 10
2.0.0 (Object#main):0 > g.player.hit_points = 12
=> 12
2.0.0 (Object#main):0 > g.gui.display_health
HEALTH: 83
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment