Class: Charosc::Markov
- Inherits:
-
Object
- Object
- Charosc::Markov
- Defined in:
- lib/charosc/markov.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#set ⇒ Object
readonly
Returns the value of attribute set.
Instance Method Summary collapse
-
#get_sequence(start_obj, depth) ⇒ Object
Public: Get a Markov-weighted random selection from possible sequences after start_obj in @set.
-
#initialize(set) ⇒ Markov
constructor
set - Array of objects.
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
#db ⇒ Object (readonly)
Returns the value of attribute db.
5 6 7 |
# File 'lib/charosc/markov.rb', line 5 def db @db end |
#set ⇒ Object (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
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 |