Created
July 12, 2011 18:14
-
-
Save monkstone/1078578 to your computer and use it in GitHub Desktop.
Non Stochastic LSystem grammar in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################# | |
# library/grammar/grammar.rb | |
# Non-stochastic grammar | |
# with unique premise/rules | |
############################################################ | |
class Grammar | |
attr_accessor :axiom, :rules | |
def initialize axiom | |
@axiom = axiom | |
@rules = Hash.new | |
end | |
def add_rule premise, rule | |
rules.store(premise, rule) | |
end | |
########################################## | |
# replace each pre char with a unique rule | |
########################################## | |
def new_production production | |
production.gsub!(/./) { |c| (r = @rules[c]) ? r : c } | |
end | |
########################################## | |
# control the number of iterations | |
# default 0, returns the axiom | |
########################################## | |
def generate repeat = 0 | |
prod = axiom | |
repeat.times do | |
prod = new_production prod | |
end | |
return prod | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment