class RubyMan::Direction

Class representing left/right/up/down/none direction in 2D space

Public Class Methods

down() click to toggle source
# File lib/ruby_man/direction.rb, line 24
def self.down
  Direction.new(:down)
end
each() { |right| ... } click to toggle source
# File lib/ruby_man/direction.rb, line 28
def self.each
  return to_enum unless block_given?
  yield Direction.right
  yield Direction.down
  yield Direction.left
  yield Direction.up
end
from_i(num) click to toggle source
# File lib/ruby_man/direction.rb, line 49
def self.from_i(num)
  case num % 4
  when 0
    return Direction.right
  when 1
    return Direction.down
  when 2
    return Direction.left
  when 3
    return Direction.up
  end
end
left() click to toggle source
# File lib/ruby_man/direction.rb, line 12
def self.left
  Direction.new(:left)
end
new(dir = :none) click to toggle source
# File lib/ruby_man/direction.rb, line 4
def initialize(dir = :none)
  @direction = dir
end
none() click to toggle source
# File lib/ruby_man/direction.rb, line 8
def self.none
  Direction.new
end
right() click to toggle source
# File lib/ruby_man/direction.rb, line 16
def self.right
  Direction.new(:right)
end
up() click to toggle source
# File lib/ruby_man/direction.rb, line 20
def self.up
  Direction.new(:up)
end

Public Instance Methods

==(other) click to toggle source
# File lib/ruby_man/direction.rb, line 90
def ==(other)
  to_i == other.to_i
end
eql?(other) click to toggle source
# File lib/ruby_man/direction.rb, line 94
def eql?(other)
  self == other
end
hash() click to toggle source
# File lib/ruby_man/direction.rb, line 98
def hash
  @direction.hash
end
inverted() click to toggle source
# File lib/ruby_man/direction.rb, line 80
def inverted
  Direction.each.select { |dir| dir != self }
end
opposite() click to toggle source
# File lib/ruby_man/direction.rb, line 74
def opposite
  i = to_i
  return Direction.none unless i
  Direction.from_i(to_i + 2)
end
rel_x() click to toggle source
# File lib/ruby_man/direction.rb, line 62
def rel_x
  i = to_i
  return 0 unless i
  (1 - i) * ((i + 1) % 2)
end
rel_y() click to toggle source
# File lib/ruby_man/direction.rb, line 68
def rel_y
  i = to_i
  return 0 unless i
  (2 - i) * (i % 2)
end
to_angle() click to toggle source
# File lib/ruby_man/direction.rb, line 84
def to_angle
  i = to_i
  return 0 unless i
  i * 90
end
to_i() click to toggle source
# File lib/ruby_man/direction.rb, line 36
def to_i
  case @direction
  when :right
    return 0
  when :down
    return 1
  when :left
    return 2
  when :up
    return 3
  end
end