Sha256: 905a67c35fdeb7ffabfc97609ec80938b5e0af836c8b009cbb193bf3d35176e4
Contents?: true
Size: 1.93 KB
Versions: 3
Compression:
Stored size: 1.93 KB
Contents
# FIXME: nest this into Jumoku's namespace and inherit from stdlib classes class Array # Takes a flat array of nodes pairs and yield # them to the block. def each_nodes_pair raise if self.size % 2 != 0 each_by(2) { |pair| yield pair } end # Fetches the positions of Branch items in an array, # and expands them in place as nodes pairs. # # expand_branches!(1,2, Branch.new(2, 3), 3,4, 4,5) # # => 1,2, 2,3, 3,4, 4,5 # # @param [*nodes, *Branch] *a a flat list of nodes pairs and branches # @return [*nodes] a flat list of nodes pairs def expand_branches! branches_positions = self.dup.map_send(:"is_a?", Jumoku::Branch).map_with_index { |e,i| i if e == true }.compact # obviously positions will gain an offset of 1 for each expanded branch, # so let's correct this right away branches_positions = branches_positions.map_with_index { |e,i| e += i } branches_positions.each do |pos| branch = self.delete_at pos self.insert pos, branch.source self.insert pos+1, branch.target end return self end def create_pairs self.map_with_index { |e,i| ((pairs ||= []) << e).concat(self.values_at(i+1)) }[0..-2] end # Creates nodes pairs from a flat array specifying unique nodes. # # b = [1, 2, 3, "foo", :bar, Array.new] # b.create_nodes_pairs # => [1, 2, 2, 3, 3, "foo", "foo", :bar, :bar, []] # # @return [Array(nodes)] def create_nodes_pairs_list self.create_pairs.flatten(1) end # Create a list of branches object based on an expandable array of nodes. # # TODO: refacto, it's painful having to pass the branch_type # # @param [DirectedBranch, UndirectedBranch] branch_type # def create_branches_list(branch_type) branches = [] self.expand_branches!.each_by(2) { |pair| branches << branch_type.new(pair[0], pair[1]) } branches end end class String def constantize to_s.gsub(/(_|^)(\w)/) { $2.upcase } end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
jumoku-0.2.5 | lib/jumoku/ext/ext.rb |
jumoku-0.2.4 | lib/jumoku/ext/ext.rb |
jumoku-0.2.3 | lib/jumoku/ext/ext.rb |