Created
December 15, 2017 00:49
-
-
Save nilesmc/e511efec5a54c33ef69f911b4484e5f7 to your computer and use it in GitHub Desktop.
Citrus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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