Skip to content

Instantly share code, notes, and snippets.

@monkstone
Created July 12, 2011 18:14
Show Gist options
  • Save monkstone/1078578 to your computer and use it in GitHub Desktop.
Save monkstone/1078578 to your computer and use it in GitHub Desktop.
Non Stochastic LSystem grammar in ruby
#############################################################
# 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