Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bredfern/37490f6f48d021a3a839911a9b6d33d3 to your computer and use it in GitHub Desktop.
Save bredfern/37490f6f48d021a3a839911a9b6d33d3 to your computer and use it in GitHub Desktop.
The polish method is used to take a really messy array of arbitrarily complex nested items and produce a simple array of integers as a result. You can statically call this from the module, SmoothArray.polish(my_complex_array).
module SmoothArray
# Sample usage:
# require SmoothArray
# ugly_array = [[1,2,[3]],4]
# result = []
# result = SmoothArray.polish(ugly_array)
# result.each do |i|
# puts i.to_s
# end
def self.polish(ugly_array)
result_array = []
process_array = []
process_array.push(ugly_array)
while (process_array.size > 0)
current_item = process_array.pop
if current_item.kind_of?(Array)
current_item.each do |nested_item|
process_array.push(nested_item)
end
else
result_array.push(current_item)
end
end
return result_array
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment