Skip to content

Instantly share code, notes, and snippets.

@nilesmc
Created December 15, 2017 00:49
Show Gist options
  • Save nilesmc/e511efec5a54c33ef69f911b4484e5f7 to your computer and use it in GitHub Desktop.
Save nilesmc/e511efec5a54c33ef69f911b4484e5f7 to your computer and use it in GitHub Desktop.
Citrus
class Squisher
def initialize(items)
@inputs = items
@outputs = []
end
attr_reader :outputs, :inputs
def build
squish(inputs)
end
private
def squish(items)
items.each do |item|
is_array?(item) ? squish(item) : add_to_output(item)
end
end
def add_to_output(input)
outputs.push(input)
end
def is_array?(input)
input.class == Array
end
end
context Squisher do
describe "#squish" do
it "a three level array should produce a flattened array" do
input = [[1,2,[3]],4]
squished = Squisher.new(input)
squished.build
expect(squished.outputs).to eq([1,2,3,4])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment