Module: FiniteMDP::VectorValued

Defined in:
lib/finite_mdp/vector_valued.rb

Overview

Define an object's hash code and equality (in the sense of eql?) according to its array representation (to_a). See notes for Model for why this might be useful.

A class that includes this module must define to_a.

Examples:


class MyPoint
  include FiniteMDP::VectorValued

  def initialize x, y
    @x, @y = x, y
  end

  attr_accessor :x, :y

  # must implement to_a to make VectorValued work
  def to_a
    [x, y]
  end
end

MyPoint.new(0, 0).eql?(MyPoint.new(0, 0)) #=> true as expected

Instance Method Summary collapse

Instance Method Details

#eql?(other) ⇒ Boolean

Redefine equality based on to_a.

Returns:

  • (Boolean)

44
45
46
# File 'lib/finite_mdp/vector_valued.rb', line 44

def eql?(other)
  to_a.eql? other.to_a
end

#hashInteger

Redefine hashing based on to_a.

Returns:

  • (Integer)

35
36
37
# File 'lib/finite_mdp/vector_valued.rb', line 35

def hash
  to_a.hash
end