Skip to content
Permalink
Browse files

Merge pull request #571 from kostya/flatten

array#flatten more tags
  • Loading branch information
alex committed Apr 10, 2013
2 parents 771db4f + 1c636e6 commit 6854b73326538b7fb1ab82e18ef61a90d3a3d543
Showing with 41 additions and 25 deletions.
  1. +10 −16 lib-topaz/array.rb
  2. +1 −0 lib-topaz/bootstrap.rb
  3. +9 −0 lib-topaz/topaz.rb
  4. +21 −0 lib-topaz/topaz/array.rb
  5. +0 −9 spec/tags/core/array/flatten_tags.txt
@@ -163,25 +163,19 @@ def first(*args)
end

def flatten(level = -1)
list = []
Thread.current.recursion_guard(:array_flatten, self) do
self.each do |item|
if level == 0
list << item
elsif ary = Array.try_convert(item)
list.concat(ary.flatten(level - 1))
else
list << item
end
end
return list
end
raise ArgumentError.new("tried to flatten recursive array")
level = Topaz.convert_type(level, Fixnum, :to_int)
out = self.class.allocate
Topaz::Array.flatten(self, out, level)
out
end

def flatten!(level = -1)
list = self.flatten(level)
self.replace(list)
raise RuntimeError.new("can't modify frozen #{self.class}") if frozen?
level = Topaz.convert_type(level, Fixnum, :to_int)
out = self.class.allocate
if Topaz::Array.flatten(self, out, level)
self.replace(out)
end
end

def sort(&block)
@@ -5,6 +5,7 @@
load(File.join(lib_topaz, file))
end

load_bootstrap.call("topaz.rb")
load_bootstrap.call("array.rb")
load_bootstrap.call("class.rb")
load_bootstrap.call("comparable.rb")
@@ -0,0 +1,9 @@
module Topaz
end

lib_topaz = File.join(File.dirname(__FILE__), 'topaz')
load_bootstrap = proc do |file|
load(File.join(lib_topaz, file))
end

load_bootstrap.call("array.rb")
@@ -0,0 +1,21 @@
module Topaz
class Array
def self.flatten(array, out, level)
modified = nil
Thread.current.recursion_guard(:array_flatten, array) do
array.each do |e|
if level == 0
out << e
elsif e.respond_to?(:to_ary) && ary = ::Array.try_convert(e)
modified = true
flatten(ary, out, level - 1)
else
out << e
end
end
return modified
end
raise ArgumentError, "tried to flatten recursive array"
end
end
end

This file was deleted.

0 comments on commit 6854b73

Please sign in to comment.
You can’t perform that action at this time.