Skip to content

Instantly share code, notes, and snippets.

@indiesquidge
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indiesquidge/a942b7265cf33e8104d4 to your computer and use it in GitHub Desktop.
Save indiesquidge/a942b7265cf33e8104d4 to your computer and use it in GitHub Desktop.
Custom Array Flatten
# Partner: Chris Luhring, @cluhring
require_relative "custom_array"
RSpec.describe "CustomArray" do
it "can flatten some custom array of numbers" do
c = CustomArray.new([[1,2],[3,[4,5]]])
expect(c.flatten).to eq([1, 2, 3, 4, 5])
end
it "can flatten some custom array of numbers" do
c = CustomArray.new([["capybara", "hamster"],["mouse",["rat", "gerbil"]]])
expect(c.flatten).to eq(["capybara", "hamster", "mouse", "rat", "gerbil"])
end
end
# ==================================================================
class CustomArray
attr_reader :array
def initialize(array)
@array = array
end
def flatten
eval("[#{array.to_s.gsub("[", "").gsub("]", "")}]")
end
end
@indiesquidge
Copy link
Author

Updated based on @worace's hacky feedback to make it work for arrays of numbers as well as strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment