From 80646a7890a0e786f7f6f1614a519eccae3b0341 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Fri, 18 Apr 2014 21:57:11 +0900 Subject: Add a block argument with Array#uniq_bang --- mrbgems/mruby-array-ext/mrblib/array.rb | 25 ++++++++++++++++++++----- mrbgems/mruby-array-ext/test/array.rb | 6 ++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 7f48811f1..0da5d05a8 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -1,7 +1,8 @@ class Array ## # call-seq: - # ary.uniq! -> ary or nil + # ary.uniq! -> ary or nil + # ary.uniq! { |item| ... } -> ary or nil # # Removes duplicate elements from +self+. # Returns nil if no changes are made (that is, no @@ -11,13 +12,27 @@ class Array # a.uniq! #=> ["a", "b", "c"] # b = [ "a", "b", "c" ] # b.uniq! #=> nil + # c = [["student","sam"], ["student","george"], ["teacher","matz"]] + # c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]] # - def uniq! + def uniq!(&block) ary = self.dup result = [] - while ary.size > 0 - result << ary.shift - ary.delete(result.last) + if block + hash = {} + while ary.size > 0 + val = ary.shift + key = block.call(val) + hash[key] = val unless hash.has_key?(key) + end + hash.each_value do |value| + result << value + end + else + while ary.size > 0 + result << ary.shift + ary.delete(result.last) + end end if result.size == self.size nil diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 9a0f25f9a..3be17f187 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -36,6 +36,12 @@ assert("Array#uniq!") do b = [ "a", "b", "c" ] assert_nil b.uniq! + + c = [["student","sam"], ["student","george"], ["teacher","matz"]] + assert_equal [["student", "sam"], ["teacher", "matz"]], c.uniq! { |s| s.first } + + d = [["student","sam"], ["teacher","matz"]] + assert_nil d.uniq! { |s| s.first } end assert("Array#uniq") do -- cgit v1.2.3 From cb5e6edb7f8fa1117192da7732446229d93993c8 Mon Sep 17 00:00:00 2001 From: Jun Hiroe Date: Fri, 18 Apr 2014 22:28:42 +0900 Subject: Add a block argument with Array#uniq --- mrbgems/mruby-array-ext/mrblib/array.rb | 14 +++++++++++--- mrbgems/mruby-array-ext/test/array.rb | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 0da5d05a8..6c47235fe 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -43,16 +43,24 @@ class Array ## # call-seq: - # ary.uniq -> new_ary + # ary.uniq -> new_ary + # ary.uniq { |item| ... } -> new_ary # # Returns a new array by removing duplicate values in +self+. # # a = [ "a", "a", "b", "b", "c" ] # a.uniq #=> ["a", "b", "c"] # - def uniq + # b = [["student","sam"], ["student","george"], ["teacher","matz"]] + # b.uniq { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]] + # + def uniq(&block) ary = self.dup - ary.uniq! + if block + ary.uniq!(&block) + else + ary.uniq! + end ary end diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 3be17f187..d157a5b4d 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -48,6 +48,9 @@ assert("Array#uniq") do a = [1, 2, 3, 1] assert_equal [1, 2, 3], a.uniq assert_equal [1, 2, 3, 1], a + + b = [["student","sam"], ["student","george"], ["teacher","matz"]] + assert_equal [["student", "sam"], ["teacher", "matz"]], b.uniq { |s| s.first } end assert("Array#-") do -- cgit v1.2.3