Sha256: ba3a20d4c5c5476715a91ca9a8274c2e9bcaba0c2f3e3df5916f9e829d51ec08

Contents?: true

Size: 613 Bytes

Versions: 8

Compression:

Stored size: 613 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.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

8 entries across 8 versions & 2 rubygems

Version Path
jruby_art-0.2.6.pre library/grammar/grammar.rb
jruby_art-0.2.4.pre library/grammar/grammar.rb
ruby-processing-2.6.3 samples/processing_app/topics/lsystems/library/grammar/grammar.rb
ruby-processing-2.6.2 samples/processing_app/topics/lsystems/library/grammar/grammar.rb
ruby-processing-2.6.1 samples/processing_app/topics/lsystems/library/grammar/grammar.rb
ruby-processing-2.6.0 samples/processing_app/topics/lsystems/library/grammar/grammar.rb
ruby-processing-2.5.1 samples/processing_app/topics/lsystems/library/grammar/grammar.rb
ruby-processing-2.5.0 samples/processing_app/topics/lsystems/library/grammar/grammar.rb