Class: Cellula::WolframCodeRule

Inherits:
Object
  • Object
show all
Defined in:
lib/cellula/rules/wolfram_code_rule.rb

Overview

Internal: Know how to apply a Wolfram code rule on a single cell.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rule_number) ⇒ WolframCodeRule

Public: Initialize a new WolframCodeRule object. See en.wikipedia.org/wiki/Wolfram_code and en.wikipedia.org/wiki/Elementary_cellular_automaton for details.

rule_number - Integer number of the wolfram code (a number between

0 and 255).


13
14
15
16
17
18
19
20
21
# File 'lib/cellula/rules/wolfram_code_rule.rb', line 13

def initialize(rule_number)
  @rule_number = rule_number
  begin
    check_rule_number
  rescue Exception => ex
    panic ex.message
  end
  @binary_string = "%08b" % @rule_number
end

Instance Attribute Details

#rule_numberObject (readonly)

Public: Get the Integer wolfram code of the rule.



24
25
26
# File 'lib/cellula/rules/wolfram_code_rule.rb', line 24

def rule_number
  @rule_number
end

Class Method Details

.wolfram_code(pattern) ⇒ Object

Get the Integer number part of a :wolfram_code_X pattern.

pattern - Wolfram code pattern as a Symbol.

Returns the Integer number part of the pattern. Exit application if the resulting number isn’t between 0 and 255.



48
49
50
51
52
53
54
55
56
# File 'lib/cellula/rules/wolfram_code_rule.rb', line 48

def self.wolfram_code(pattern)
  pattern = pattern.to_s
  num = pattern.delete("wolfram_code_").to_i
  if num < 0 || num > 255
    panic "Bad wolfram code: #{pattern}"
  else
    num
  end
end

.wolfram_code?(symb) ⇒ Boolean

Tells if a symbol follow the :wolfram_code_X pattern.

symb - The Symbol to test.

Returns Boolean true if sym is a :wolfram_code_X pattern.

Returns:

  • (Boolean)


63
64
65
# File 'lib/cellula/rules/wolfram_code_rule.rb', line 63

def self.wolfram_code?(symb)
  symb.is_a?(Symbol) && symb.to_s.start_with?("wolfram_code_")
end

Instance Method Details

#apply_rule(cell_number, grid, study) ⇒ Object

Public: Apply the rule on a single cell.

cell_number - Integer number of the cell to apply the rule on. grid - The entire grid as an Array of Integer. study - Study instance.

Returns the Integer new state of the cell for the next generation.



33
34
35
36
37
38
39
40
# File 'lib/cellula/rules/wolfram_code_rule.rb', line 33

def apply_rule(cell_number, grid, study)
  @cell_number = cell_number
  @grid = grid
  case study.method
  when :random then apply_rule_with_random_method
  when :single then apply_rule_with_single_method
  end
end