[#37892] 配列の重複検出用Hashの使いまわし — wanabe <s.wanabe@...>
ワナベと申します。
[#37898] [Bug #1105] Ruby1.9でのrescue節の例外ハンドラのマッチの処理 — Tatsuji Kawai <redmine@...>
Bug #1105: Ruby1.9でのrescue節の例外ハンドラのマッチの処理
[#37910] [Bug:1.9] lack consistency in hash iteration — Yusuke ENDOH <mame@...>
遠藤です。
まつもと ゆきひろです
[#37918] [BUG: 1.9] encoding warning — SASADA Koichi <ko1@...>
ささだです.
[#37921] [Feature:trunk] with_index_from — Yusuke ENDOH <mame@...>
遠藤です。
At Thu, 5 Feb 2009 23:18:49 +0900,
遠藤です。
At Fri, 6 Feb 2009 00:58:59 +0900,
[#37936] zombie processes by drb tests — Tanaka Akira <akr@...>
OpenBSD で、test-all をすると、drb のところで、テストに 100
咳といいます。
[#37950] [Bug:1.9] compile error on win32ole with Mingw of MacPorts — 中田 伸悦 <nobu@...>
なかだです。
[#37956] proposal: Module#method_adding — SASADA Koichi <ko1@...>
ささだです.
[#37959] [Bug:trunk] I can modify literals — Yusuke ENDOH <mame@...>
遠藤です。
[#37980] Re: [ruby-changes:10687] Ruby:r22250 (trunk): * iseq.c (simple_default_value): allow plain strings as default — SASADA Koichi <ko1@...>
ささだです.
[#37995] Add POSTARG support to rb_scan_args() — Akinori MUSHA <akinori.musha@...>
rb_scan_args()をPOSTARG対応にするパッチです。
[#37998] [Feature:1.9] {Array,Enumerable}#uniq_by, #uniq_by! — Nobuyoshi Nakada <nobu@...>
なかだです。
[#38005] Is URI.decode() broken? — MOROHASHI Kyosuke <moronatural@...>
もろはしです。いつもお世話になっております。
なかだです。
成瀬です、
xibbarこと藤岡です。
成瀬です。
NARUSE, Yui さんは書きました:
成瀬です。
(2009年03月03日 22:45), NARUSE, Yui さんは書きました:
成瀬です。
In article <4A9E44DD.6050706@airemix.jp>,
成瀬です。
In article <4AA2CF8A.6070700@airemix.jp>,
小崎@思いつきを適当に書いてみるテスト
In article <20090907091830.2C7A.A69D9226@jp.fujitsu.com>,
> In article <20090907091830.2C7A.A69D9226@jp.fujitsu.com>,
2009/09/07 14:38, Tanaka Akira wrote:
In article <4AA5EA67.1040504@airemix.jp>,
[#38007] [Feature #1159] StringScanner に文字ベースでのインデックスを返すメソッドがほしい — Akira Matsuda <redmine@...>
Feature #1159: StringScanner に文字ベースでのインデックスを返すメソッドがほしい
[#38018] circular require in openssl — Tanaka Akira <akr@...>
以下のように、openssl には環状の require があり、警告が出ます。
In article <87vdrcul7y.fsf@fsij.org>,
まつもと ゆきひろです
In article <E1LYyoE-0005P0-Hi@x61.netlab.jp>,
[#38022] ENCODING_FIXED と ENCODING_NONE の廃止 — "NARUSE, Yui" <naruse@...>
成瀬です。
In article <49986A0A.5060602@airemix.jp>,
成瀬です。
In article <49995412.6040000@airemix.jp>,
[#38048] Add option hash support to rb_scan_args() — "Akinori MUSHA" <knu@...>
rb_scan_args() にoption hash対応を組み込むのはどうでしょうか。
[#38067] Re: [ruby-cvs:29304] Ruby:r22086 (trunk): * ruby.c (process_options): set initial default_external before -r. — "Yugui (Yuki Sonoda)" <yugui@...>
Yuguiです。
[#38075] [Bug #1198] corrupted iteratoin during "enum_for :inject" — Shyouhei Urabe <redmine@...>
Bug #1198: corrupted iteratoin during "enum_for :inject"
[#38080] [Feature:trunk] nested loop construct — Yukihiro Matsumoto <matz@...>
まつもと ゆきひろです
ささだです.
[#38096] 多重代入やメソッド引数の展開でto_aが呼ばれます — nagachika <nagachika00@...>
nagachika と申します。
前田です。
まつもと ゆきひろです
前田です。
In article <704d5db90907141754p285e6e51xdd3208b27d556906@mail.gmail.com>,
[#38098] ブロック引数と括弧・引数なしsuper — Shugo Maeda <shugo@...>
前田です。
まつもと ゆきひろです
[ruby-dev:37921] [Feature:trunk] with_index_from
遠藤です。
with_index の開始インデックスを指定できるバージョンがあると便利だと
思います。
$ ./ruby -e 'p %w(a b c).map.with_index_from(1) {|c, i| [c, i] }'
[["a", 1], ["b", 2], ["c", 3]]
インデックスを指定したい理由としては、
- ユーザインターフェイスでは 1 番目から数えることが多い
(参照) http://d.hatena.ne.jp/rubikitch/20080524/1211627661
- 列や行列の 0 番目を (初期値などとして) 特別扱いするアルゴリズムがある
(参照) http://d.hatena.ne.jp/ku-ma-me/20081026/p2
- 先頭を飛ばしてインデックス付きイテレートするのが簡単になる
(例) ary.each_with_index {|x, i| next if i == 0; ... } や
ary.drop(1).each_with_index {|x, i| i += 1; ... } が
ary.drop(1).each_with_index_from(1) {|x, y| ... } になってすっきり
などがあります。
以下のパッチは 3 つのメソッドを追加します。
- Enumerator#each_with_index_from(n)
- Enumerator#with_index_from(n)
- Enumerable#each_with_index_from(n)
どうでしょうか。
Index: enumerator.c
===================================================================
--- enumerator.c (revision 22080)
+++ enumerator.c (working copy)
@@ -397,31 +397,20 @@
}
static VALUE
-enumerator_with_index_i(VALUE val, VALUE *memo)
+enumerator_with_index_i(VALUE val, long *memo)
{
val = rb_yield_values(2, val, INT2FIX(*memo));
++*memo;
return val;
}
-/*
- * call-seq:
- * e.with_index {|(*args), idx| ... }
- * e.with_index
- *
- * Iterates the given block for each element with an index, which
- * start from 0. If no block is given, returns an enumerator.
- *
- */
static VALUE
-enumerator_with_index(VALUE obj)
+enumerator_with_index_core(VALUE obj, long memo)
{
struct enumerator *e;
- VALUE memo = 0;
int argc = 0;
VALUE *argv = 0;
- RETURN_ENUMERATOR(obj, 0, 0);
e = enumerator_ptr(obj);
if (e->args) {
argc = RARRAY_LEN(e->args);
@@ -431,7 +420,39 @@
enumerator_with_index_i, (VALUE)&memo);
}
+/*
+ * call-seq:
+ * e.with_index {|(*args), idx| ... }
+ * e.with_index
+ *
+ * Iterates the given block for each element with an index, which
+ * starts from 0. If no block is given, returns an enumerator.
+ *
+ */
static VALUE
+enumerator_with_index(VALUE obj)
+{
+ RETURN_ENUMERATOR(obj, 0, 0);
+ return enumerator_with_index_core(obj, 0);
+}
+
+/*
+ * call-seq:
+ * e.with_index_from(n) {|(*args), idx| ... }
+ * e.with_index_from(n)
+ *
+ * Iterates the given block for each element with an index, which
+ * starts from <i>n</i>. If no block is given, returns an enumerator.
+ *
+ */
+static VALUE
+enumerator_with_index_from(VALUE obj, VALUE val)
+{
+ RETURN_ENUMERATOR(obj, 1, &val);
+ return enumerator_with_index_core(obj, NUM2LONG(val));
+}
+
+static VALUE
enumerator_with_object_i(VALUE val, VALUE memo)
{
return rb_yield_values(2, val, memo);
@@ -842,8 +863,10 @@
rb_define_method(rb_cEnumerator, "initialize_copy",
enumerator_init_copy, 1);
rb_define_method(rb_cEnumerator, "each", enumerator_each, 0);
rb_define_method(rb_cEnumerator, "each_with_index",
enumerator_with_index, 0);
+ rb_define_method(rb_cEnumerator, "each_with_index_from",
enumerator_with_index_from, 1);
rb_define_method(rb_cEnumerator, "each_with_object",
enumerator_with_object, 1);
rb_define_method(rb_cEnumerator, "with_index", enumerator_with_index, 0);
+ rb_define_method(rb_cEnumerator, "with_index_from",
enumerator_with_index_from, 1);
rb_define_method(rb_cEnumerator, "with_object", enumerator_with_object, 1);
rb_define_method(rb_cEnumerator, "next", enumerator_next, 0);
rb_define_method(rb_cEnumerator, "rewind", enumerator_rewind, 0);
Index: enum.c
===================================================================
--- enum.c (revision 22080)
+++ enum.c (working copy)
@@ -1425,7 +1425,34 @@
return obj;
}
+/*
+ * call-seq:
+ * enum.each_with_index_from(n) {|obj, i| block } -> enum
+ *
+ * Calls <em>block</em> with two arguments, the item and its index,
+ * for each item in <i>enum</i>. The index starts from <i>n</i>.
+ * Given arguments are passed through to #each().
+ *
+ * hash = Hash.new
+ * %w(cat dog wombat).each_with_index_from(1) {|item, index|
+ * hash[item] = index
+ * }
+ * hash #=> {"cat"=>1, "dog"=>2, "wombat"=>3}
+ *
+ */
+static VALUE
+enum_each_with_index_from(VALUE obj, VALUE val)
+{
+ long memo = NUM2LONG(val);
+
+ RETURN_ENUMERATOR(obj, 1, &val);
+
+ rb_block_call(obj, id_each, 0, 0, each_with_index_i, (VALUE)&memo);
+ return obj;
+}
+
+
/*
* call-seq:
* enum.reverse_each {|item| block }
@@ -1845,6 +1872,7 @@
rb_define_method(rb_mEnumerable, "member?", enum_member, 1);
rb_define_method(rb_mEnumerable, "include?", enum_member, 1);
rb_define_method(rb_mEnumerable, "each_with_index",
enum_each_with_index, -1);
+ rb_define_method(rb_mEnumerable, "each_with_index_from",
enum_each_with_index_from, 1);
rb_define_method(rb_mEnumerable, "reverse_each", enum_reverse_each, -1);
rb_define_method(rb_mEnumerable, "zip", enum_zip, -1);
rb_define_method(rb_mEnumerable, "take", enum_take, 1);
--
Yusuke ENDOH <mame@tsg.ne.jp>