Class: Q_Writer
- Inherits:
-
Object
- Object
- Q_Writer
- Defined in:
- lib/q-language/writer.rb
Overview
Copyright © 2010-2011 Jesse Sielaff
Instance Method Summary collapse
-
#generate(remaining_depth = self.nesting) ⇒ Object
• User method Returns a String representing a random block of Q script.
-
#language ⇒ Object
• User method Returns “Q”.
-
#literals(literals = nil) ⇒ Object
• User method Sets the Array of literals to be sampled from when generating a random block, then returns the Q_Writer.
-
#methods(methods = nil) ⇒ Object
• User method Sets the Array of methods to be sampled from when generating a random block, then returns the Q_Writer.
-
#nesting(nesting = nil) ⇒ Object
• User method Sets the maximum depth of nested blocks in a random script, then returns the Q_Writer.
-
#nodes(nodes = nil) ⇒ Object
• User method Sets the maximum number of nodes per block in a random script, then returns the Q_Writer.
-
#variables(variables = nil) ⇒ Object
• User method Sets the Array of variables to be sampled from when generating a random block, then returns the Q_Writer.
-
#weight(weight = nil) ⇒ Object
• User method Sets the relative frequency with which each node type will appear when generating a random block, then returns the Q_Writer.
Instance Method Details
#generate(remaining_depth = self.nesting) ⇒ Object
• User method Returns a String representing a random block of Q script.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/q-language/writer.rb', line 10 def generate (remaining_depth = self.nesting) b = 0...weight[:block] l = b.end...(b.end + weight[:literal]) m = l.end...(l.end + weight[:method]) v = m.end...(m.end + weight[:variable]) strings = nodes.times.map do case rand(v.end) when b then (remaining_depth > 0) ? generate(remaining_depth - 1) : redo when l then literals.sample.to_qliteral when m then methods.sample when v then (x = variables.sample) ? ":#{x}" : () end end "{#{strings.compact.join ?\ }}" end |
#language ⇒ Object
• User method Returns “Q”.
31 32 33 |
# File 'lib/q-language/writer.rb', line 31 def language ?Q end |
#literals(literals = nil) ⇒ Object
• User method Sets the Array of literals to be sampled from when generating a random block, then returns the Q_Writer. If no argument is given, returns the current literals Array. Default value is [-1,0,1,2,5,10,‘abc’,:foo,:bar].
40 41 42 |
# File 'lib/q-language/writer.rb', line 40 def literals (literals = nil) literals ? (@literals = literals; self) : (@literals ||= [-1,0,1,2,5,10,'abc',:foo,:bar]) end |
#methods(methods = nil) ⇒ Object
• User method Sets the Array of methods to be sampled from when generating a random block, then returns the Q_Writer. If no argument is given, returns the current methods Array. Default value is the Array of all available Q methods.
49 50 51 |
# File 'lib/q-language/writer.rb', line 49 def methods (methods = nil) methods ? (@methods = methods.map(&:to_sym); self) : (@methods ||= Q_Object.method_names) end |
#nesting(nesting = nil) ⇒ Object
• User method Sets the maximum depth of nested blocks in a random script, then returns the Q_Writer. If no argument is given, returns the current maximum depth of nesting. Default value is 5.
58 59 60 |
# File 'lib/q-language/writer.rb', line 58 def nesting (nesting = nil) nesting ? (@nesting = nesting.to_int; self) : (@nesting ||= 5) end |
#nodes(nodes = nil) ⇒ Object
• User method Sets the maximum number of nodes per block in a random script, then returns the Q_Writer. A block node counts as only 1 node, ignoring the nodes inside the block. If no argument is given, gives the current maximum nodes per block. Default value is 7.
68 69 70 |
# File 'lib/q-language/writer.rb', line 68 def nodes (nodes = nil) nodes ? (@nodes = nodes.to_int; self) : (@nodes ||= 7) end |
#variables(variables = nil) ⇒ Object
• User method Sets the Array of variables to be sampled from when generating a random block, then returns the Q_Writer. If no argument is given, returns the current variables Array. Default value is [:i,:j,:k,:l,:m,:n,:w,:x,:y,:z].
77 78 79 |
# File 'lib/q-language/writer.rb', line 77 def variables (variables = nil) variables ? (@variables = variables.map(&:to_sym); self) : (@variables ||= %w:i j k l m n w x y z:.map(&:to_sym)) end |
#weight(weight = nil) ⇒ Object
• User method Sets the relative frequency with which each node type will appear when generating a random block, then returns the Q_Writer. If no argument is given, returns the current node weight. Default values are block: 1, literal: 1, method: 1, variable: 1.
87 88 89 |
# File 'lib/q-language/writer.rb', line 87 def weight (weight = nil) weight ? (@weight = weight.to_hash.select {|k,v| [:block, :literal, :method, :variable].include?(k) && v.is_a?(Fixnum) }; @weight.default = 1; self) : (@weight ||= {block: 1, literal: 1, method: 1, variable: 1}) end |