Skip to content

Instantly share code, notes, and snippets.

@ledsun
Forked from Shinpeim/player_01.rb
Last active December 12, 2015 08:59
Show Gist options
  • Select an option

  • Save ledsun/4748486 to your computer and use it in GitHub Desktop.

Select an option

Save ledsun/4748486 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class Player
attr_reader :position
def initialize(position)
@position = position
end
def move(direction)
case direction
when :up
@position[:y] -= 10
when :down
@position[:y] += 10
when :left
@position[:x] -= 10
when :right
@position[:x] += 10
end
end
end
class FastPlayer < Player
def move(direction)
super
super
end
end
player = Player.new(:x => 100, :y => 100)
player.move(:up)
p player.position # => {:x => 100, :y => 90}
player = FastPlayer.new player.position
player.move(:down)
p player.position #=> {:x => 100, :y => 110}
@ledsun
Copy link
Author

ledsun commented Feb 10, 2013

継承バージョン

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment