[ruby-dev:50787] [Ruby trunk Feature#1153] Enumerable#uniq

From: shyouhei@...
Date: 2019-05-20 07:28:05 UTC
List: ruby-dev #50787
Issue #1153 has been updated by shyouhei (Shyouhei Urabe).

Status changed from Rejected to Closed
Description updated

This is accepted & implemented as per #11090.

----------------------------------------
Feature #1153: Enumerable#uniq
https://bugs.ruby-lang.org/issues/1153#change-78091

* Author: nobu (Nobuyoshi Nakada)
* Status: Closed
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
=begin
 なかだです。
 
 Arrayだけでなく、Enumerableにもuniqがあってもいいんじゃないでしょ
 うか。
 
 
 Index: enum.c
 ===================================================================
 --- enum.c	(revision 22100)
 +++ enum.c	(working copy)
 @@ -1794,4 +1794,29 @@ enum_cycle(int argc, VALUE *argv, VALUE 
  }
  
 +static VALUE
 +enum_uniq_i(VALUE i, VALUE hash, int argc, VALUE *argv)
 +{
 +    return rb_hash_aset(hash, i, Qtrue);
 +}
 +
 +static int
 +push_key(st_data_t key, st_data_t val, st_data_t ary)
 +{
 +    rb_ary_push((VALUE)ary, (VALUE)key);
 +    return ST_DELETE;
 +}
 +
 +static VALUE
 +enum_uniq(VALUE obj)
 +{
 +    VALUE hash = rb_hash_new(), uniq;
 +
 +    RBASIC(hash)->klass = 0;
 +    rb_block_call(obj, id_each, 0, 0, enum_uniq_i, hash);
 +    uniq = rb_ary_new2(RHASH_SIZE(hash));
 +    st_foreach(RHASH_TBL(hash), push_key, uniq);
 +    return uniq;
 +}
 +
  /*
   *  The <code>Enumerable</code> mixin provides collection classes with
 @@ -1853,4 +1878,5 @@ Init_Enumerable(void)
      rb_define_method(rb_mEnumerable, "drop_while", enum_drop_while, 0);
      rb_define_method(rb_mEnumerable, "cycle", enum_cycle, -1);
 +    rb_define_method(rb_mEnumerable, "uniq", enum_uniq, 0);
  
      id_eqq  = rb_intern("===");
 
 
 -- 
 --- 僕の前にBugはない。
 --- 僕の後ろにBugはできる。
     中田 伸悦
=end




-- 
https://bugs.ruby-lang.org/

In This Thread

Prev Next