class Crispr::Mutations::Array

Provides mutations for Ruby array nodes.

def mutations_for(node)

Returns:
  • (Array) - list of mutated nodes

Parameters:
  • node (Parser::AST::Node) -- the array node to mutate
def mutations_for(node)
  return [] unless node.is_a?(Parser::AST::Node) && node.type == :array
  mutations = []
  # Remove each element individually
  node.children.each_with_index do |_, index|
    new_elements = node.children.dup
    new_elements.delete_at(index)
    mutations << s(:array, *new_elements)
  end
  # Add nil to the end
  mutations << s(:array, *node.children, s(:nil))
  # Replace each element individually with nil
  node.children.each_with_index do |_, index|
    new_elements = node.children.dup
    new_elements[index] = s(:nil)
    mutations << s(:array, *new_elements)
  end
  # Replace all elements with nil
  mutations << s(:array, *::Array.new(node.children.size) { s(:nil) })
  mutations
end