class Bones::Common

This class is created to be a parent class of the Bones engine, the Bones species and the Bones algorithm class.

Public Instance Methods

flatten_hash(hash,flat_hash={},prefix='') click to toggle source

This method flattens a multidimensional hash into a one dimensional hash. This method is called recursively.

    # File lib/bones.rb
152 def flatten_hash(hash,flat_hash={},prefix='')
153         hash.each do |key,value|
154                 if value.is_a?(Hash)
155                         flatten_hash(value,flat_hash,prefix+key.to_s+'_')
156                 else
157                         flat_hash[(prefix+key.to_s).to_sym] = value
158                 end
159         end
160         return flat_hash
161 end
from(range) click to toggle source

Helper to obtain the ‘from’ part from a range. For example, the method will yield ‘1’ if applied to ‘1:N-1’.

    # File lib/bones.rb
124 def from(range)
125         return '('+simplify(range.split(RANGE_SEP)[0])+')'
126 end
replace_defines(original_code,defines) click to toggle source

Method to process the defines in a piece of code. The code must be formatted as a string. It returns a copy of the code with all defines replaced.

    # File lib/bones.rb
190 def replace_defines(original_code,defines)
191         code = original_code.clone
192         list = defines.sort_by { |key,value| key.to_s.length }
193         list.reverse.each do |pair|
194                 search = pair[0].to_s
195                 replace = pair[1]
196                 code.gsub!(search,replace)
197         end
198         return code
199 end
search_and_replace(hash,code) click to toggle source

This method calls search_and_replace! to replaces markers in code with a hash of search-and-replace values. Before, it clones the existing code so that the original copy is maintained.

    # File lib/bones.rb
181 def search_and_replace(hash,code)
182         new_code = code.clone
183         search_and_replace!(hash,new_code)
184         return new_code
185 end
search_and_replace!(hash,code) click to toggle source

This method performs a search-and-replace. It searches for the <index> of the input hash and replaces it with the corresponding key. It searches for to-be-replaced variables of the form ‘<name>’. If such patterns still occur after searching and replacing, the method raises an error.

    # File lib/bones.rb
169 def search_and_replace!(hash,code)
170         flat_hash = flatten_hash(hash)
171         2.times do
172                 flat_hash.each { |search,replace| code.gsub!(SAR_MARKER1+search.to_s+SAR_MARKER2,replace) }
173         end
174         raise_error('Unrecognized replace variable "'+($~).to_s+'" in the skeleton library') if code =~ /<[a-zA-Z]+\w*>/
175 end
sum(range) click to toggle source

Helper to obtain the sum of a range. For example, the method will yield ‘N-2’ if applied to ‘1:N-1’. There is a check to ensure that the range is correct.

    # File lib/bones.rb
138 def sum(range)
139         raise_error('Incorrect range given: "'+range+'"') if range.split(RANGE_SEP).length != 2
140         return '('+simplify("(#{to(range)}-#{from(range)}+1)")+')'
141 end
sum_and_from(range) click to toggle source

Helper to obtain the sum and ‘from’ part of a range. For example, the method will yield ‘((N-2)+1)’ if applied to ‘1:N-1’.

    # File lib/bones.rb
146 def sum_and_from(range)
147         return '('+simplify(sum(range)+'+'+from(range))+')'
148 end
to(range) click to toggle source

Helper to obtain the ‘to’ part from a range. For example, the method will yield ‘N-1’ if applied to ‘1:N-1’.

    # File lib/bones.rb
131 def to(range)
132         return '('+simplify(range.split(RANGE_SEP)[1])+')'
133 end