Skip to content

Instantly share code, notes, and snippets.

@EdDeAlmeidaJr
Last active May 13, 2016 11:34
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 EdDeAlmeidaJr/542b518a61fe0601c396943a28c50533 to your computer and use it in GitHub Desktop.
Save EdDeAlmeidaJr/542b518a61fe0601c396943a28c50533 to your computer and use it in GitHub Desktop.
Flatten an array without using Array#flatten
def ints_only?(arr)
res = true
arr.map { |item| res = res && item.is_a?(Integer) ? true : false }
res
end
def array_flatten(arr)
while !ints_only?(arr) do
new_arr = Array.new
arr.map{ |item|
if item.is_a?(Integer) then
new_arr.push(item)
else
item.map{ |sitem| new_arr.push(sitem) }
end
arr = new_arr
}
end
arr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment