Skip to content

Instantly share code, notes, and snippets.

@bchase
Last active October 1, 2018 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bchase/23124daead533449568a0697c7a12cf5 to your computer and use it in GitHub Desktop.
Save bchase/23124daead533449568a0697c7a12cf5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Pawn
def move_down?(x,y)
can_move_distance?(x,y)
end
def move_up?(x,y)
can_move_distance?(x,y)
end
private
def can_move_distance?(x,y)
# actually implemented in reality,
# but `true` here to contrast `false` below
true
end
end
class WhitePawn < Pawn
def move_down?(x,y)
false
end
end
class BlackPawn < Pawn
def move_up?(x,y)
false
end
end
white = WhitePawn.new
black = BlackPawn.new
puts "WHITE DOWN -- #{ white.move_down?(5,5) }"
puts "WHITE UP -- #{ white.move_up?(5,5) }"
puts "BLACK DOWN -- #{ black.move_down?(5,5) }"
puts "BLACK UP -- #{ black.move_up?(5,5) }"
# $ ruby chess-pawn.rb
# WHITE DOWN -- false
# WHITE UP -- true
# BLACK DOWN -- true
# BLACK UP -- false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment