Skip to content

Instantly share code, notes, and snippets.

@Ryanspink1
Last active December 19, 2017 22:55
Show Gist options
  • Save Ryanspink1/4a6ada33500e96686edcdd7c2e4a5935 to your computer and use it in GitHub Desktop.
Save Ryanspink1/4a6ada33500e96686edcdd7c2e4a5935 to your computer and use it in GitHub Desktop.
Ruby Flatten

RubyFlatten README

This program is a clone of the Ruby method "Flatten".
It may be cloned from the source repository at https://github.com/Ryanspink1/ruby_flatten

Testing:

This application uses Minitest for testing, please make sure it's installed.
To test from root directory, type "ruby test/flatten_test.rb" into terminal

Will output 12 runs, 20 assertions

Running application in terminal:

Program will only accept a single array of integers in any combination of arrays.

Program will output a single flattened array of integers in the order in which they appeared in the input array.

From program root directory in terminal:

  1. irb
  2. require './lib/flatten'
  3. RubyFlatten.start(input_array)
class RubyFlatten
attr_reader :output
def initialize
@output = []
end
def self.start(input)
c = RubyFlatten.new
c.check_input_type(input)
c.flatten_this(input)
c.output
end
def flatten_this(input)
input.each {|n| check_for_array(n)}
end
def check_for_array(n)
if n.class == Array
flatten_this(n)
else
check_output_type(n)
output << n
end
end
def check_input_type(input)
raise("Input not an array") if input.class != Array
end
def check_output_type(input)
raise("Input not an integer") if input.class != Integer
end
end
require 'minitest/autorun'
require 'minitest/pride'
require './lib/flatten'
class FlattenTest < Minitest::Test
attr_reader :output
def setup
@output = RubyFlatten.start([1,[2,[3,[4,5]]],6,[7,8]])
end
def test_will_not_accept_string
assert_raises("Input not an array") {RubyFlatten.start("Bad Input")}
end
def test_will_not_accept_hash
assert_raises("Input not an array") {RubyFlatten.start({"Bad Input": true})}
end
def test_will_not_accept_lone_integer
assert_raises("Input not an array") {RubyFlatten.start(1)}
end
def test_will_not_accept_lone_float
assert_raises("Input not an array") {RubyFlatten.start(1.23)}
end
def test_accepts_negative_integers
expected = [1,-2,3,4,-5,6,7,8]
output = RubyFlatten.start([1,[-2,[3,[4,-5]]],6,[7,8]])
assert_equal expected, output
end
def test_output_not_nil
expected = output.nil?
assert_equal false, expected
end
def test_output_not_empty
expected = output.empty?
assert_equal false, expected
end
def test_output_is_an_array
expected = output.class
assert_equal Array, expected
end
def test_each_index_is_an_integer
output.each do |index|
output_class = index.class
assert_equal Integer, output_class
end
end
def test_output_count
count = output.count
assert_equal 8, count
assert_equal false, output.count == 3
end
def test_output_is_correct
expected = [1,2,3,4,5,6,7,8]
assert_equal expected, output
end
def test_output_is_not_random
assert_equal false, output == [8,7,3,4,1,2,6,5]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment