Sha256: 10df575ca4c79f9e62cbb9e164cac222e2cac7c218b9e3b543c363f52130944b

Contents?: true

Size: 626 Bytes

Versions: 4

Compression:

Stored size: 626 Bytes

Contents

############################
# Simple lsystem grammar
############################
class Grammar
  
  attr_reader :axiom, :rules
  def initialize(axiom, rules)
    @axiom = axiom
    @rules = rules
  end

  def expand(production, iterations, &block)
    production.each_char do |token|
      if rules.has_key?(token) && iterations > 0        
        expand(rules[token], iterations - 1, &block)
      else
        yield token
      end
    end
  end

  def each gen
    expand(axiom, gen) {|token| yield token }
  end

  def generate gen
    output = []
    each(gen){ |token| output << token }
    return output
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-processing-2.4.4 samples/processing_app/topics/lsystems/library/grammar/grammar.rb
ruby-processing-2.4.3 samples/processing_app/topics/lsystems/library/grammar/grammar.rb
ruby-processing-2.4.2 samples/processing_app/topics/lsystems/library/grammar/grammar.rb
ruby-processing-2.4.1 samples/processing_app/topics/lsystems/library/grammar/grammar.rb