Skip to content

Instantly share code, notes, and snippets.

@phoenixwizard
Created November 8, 2016 08:33
Show Gist options
  • Save phoenixwizard/73519ebd19ee7cef6267ce972483214c to your computer and use it in GitHub Desktop.
Save phoenixwizard/73519ebd19ee7cef6267ce972483214c to your computer and use it in GitHub Desktop.
Array Flatten
require 'json'
def flatten_current_array(array,new_array)
if array.class == Array
array.each do |elem|
flatten_current_array(elem,new_array)
end
else
new_array.push(array)
end
end
begin
input_string = gets.strip
input_array=JSON.parse input_string
flattened_array=[]
flatten_current_array(input_array,flattened_array)
puts flattened_array.to_s
rescue Exception => e
puts "Error Message: #{e.message}"
puts "Error Trace: #{e.backtrace.inspect}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment