* [ruby-dev:50787] [Ruby trunk Feature#1153] Enumerable#uniq
[not found] <redmine.issue-1153.20090213221901@ruby-lang.org>
@ 2019-05-20 7:28 ` shyouhei
0 siblings, 0 replies; only message in thread
From: shyouhei @ 2019-05-20 7:28 UTC (permalink / raw)
To: ruby-dev
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があってもいいんじゃないでしょ
うか。
\f
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("===");
\f
--
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
中田 伸悦
=end
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2019-05-20 7:28 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <redmine.issue-1153.20090213221901@ruby-lang.org>
2019-05-20 7:28 ` [ruby-dev:50787] [Ruby trunk Feature#1153] Enumerable#uniq shyouhei
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).