Module: DRP::InstanceMethods

Defined in:
lib/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#default_rule_method(*args) ⇒ Object

this is called when all rule methods are exhausted during the selection process. by default returns nil. you can override this in your extended class, but as a regular method not a rule method to have non-nil value returned, but be sure to accept an array of *args or have the arity correct to handle all your rule methods.



33
# File 'lib/instance_methods.rb', line 33

def default_rule_method *args; end

#depthObject

how deep is the current rule method’s recursion



60
61
62
# File 'lib/instance_methods.rb', line 60

def depth
  @__drp__depth__stack.last
end

#map(rng = nil, function = :linear, &b) ⇒ Object

uses next_codon to output to somewhere within the range specified using specified function, unless a block is given in which case it counts the formal parameters to the block, and yields appropriate number of codons using next_codon you may not pass both a block and a range, only one or the other



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/instance_methods.rb', line 86

def map rng = nil, function = :linear, &b # :yields: next_codon ... 
  if block_given?
    if rng
      raise ArgumentError, "both block and #{rng} passed to map", caller
    end
    arity = b.arity
    case arity
      # these are here, and also ordered, for efficiencies sake
      when 1
        yield next_codon
      when 2
        yield next_codon, next_codon
      when 0, -1
        raise ArgumentError, 'block given to map must have 1 or more arguments', caller
      else
        yield *Array.new(arity) { next_codon }
    end
  else
    Utils::map rng, next_codon, function
  end
end

#max_depthObject

what is the maximum depth attainable by the current rule method. do not confuse this with the class method setter



66
67
68
# File 'lib/instance_methods.rb', line 66

def max_depth
  @__drp__rule__method__stack.last.max_depth
end

#next_codonObject

you should reimplement this method in your extended class, that is unless you want the default behaviour of an endless random stream. this is included mostly for quick testing purposes



48
49
50
# File 'lib/instance_methods.rb', line 48

def next_codon
  rand
end

#next_meta_codonObject

this is what weights and max_depths use to get codons. it defaults to just using next_codon. override it in your extended class to have them use a separate codon stream



55
56
57
# File 'lib/instance_methods.rb', line 55

def next_meta_codon
  next_codon
end