Class: Charosc::Markov

Inherits:
Object
  • Object
show all
Defined in:
lib/charosc/markov.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(set) ⇒ Markov

set - Array of objects



8
9
10
11
# File 'lib/charosc/markov.rb', line 8

def initialize(set)
  @set = set
  @db  = {}
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



5
6
7
# File 'lib/charosc/markov.rb', line 5

def db
  @db
end

#setObject (readonly)

Returns the value of attribute set.



5
6
7
# File 'lib/charosc/markov.rb', line 5

def set
  @set
end

Instance Method Details

#get_sequence(start_obj, depth) ⇒ Object

Public: Get a Markov-weighted random selection from possible sequences

after start_obj in @set. Memoizes randomizers on @db

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/charosc/markov.rb', line 16

def get_sequence(start_obj, depth)
  raise ArgumentError, "depth must be a number > 1" unless depth > 1

  @db[start_obj] ||= {}
  (@db[start_obj][depth] ||= build_randomizer(start_obj, depth)).sample
end