Last active
August 29, 2015 14:20
-
-
Save indiesquidge/a942b7265cf33e8104d4 to your computer and use it in GitHub Desktop.
Custom Array Flatten
This file contains 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated based on @worace's hacky feedback to make it work for arrays of numbers as well as strings.