[#47049] fileのopenに関する動作についての質問 — 高橋作郎 <sakurou3960@...>

はじめまして。

14 messages 2010/05/04

[ruby-list:47064] match メソッドの pos オプションを指定した場合の \A のマッチ

From: "KISHIMOTO, Makoto" <ksmakoto@...4u.or.jp>
Date: 2010-05-06 05:37:02 UTC
List: ruby-list #47064
きしもとです

※ String#match の rdoc がオプショナル引数に対応してないようです

ruby 1.9 の Regexp#match および String#match のオプショナル引数で、
開始位置を指定した場合の \A のマッチについてなのですが、現在の実装では、
\A はあくまでも文字列の先頭にのみマッチし、開始位置にはマッチしません。

$ irb19
irb(main):001:0> "foo".match /\A/, 1
=> nil

文字列の中に現れるパターンを、開始位置をずらしながらなめるような処理を
書く時に、開始位置でのみマッチさせたい、というわけで、マッチしてくれた
ほうがうれしいように思うのですが、現在の実装が意図したもので、仕様という
ことなんでしょうか?

以下は ※ についてのパッチです

Index: string.c
===================================================================
--- string.c	(revision 27637)
+++ string.c	(working copy)
@@ -2555,12 +2555,15 @@
 
 /*
  *  call-seq:
- *     str.match(pattern)   => matchdata or nil
+ *     str.match(pattern)        => matchdata or nil
+ *     str.match(pattern, pos)   => matchdata or nil
  *
  *  Converts <i>pattern</i> to a <code>Regexp</code> (if it isn't already one),
  *  then invokes its <code>match</code> method on <i>str</i>.  If the second
  *  parameter is present, it specifies the position in the string to begin the
  *  search.
+ *  If the second parameter is present, it specifies the position in the string
+ *  to begin the search.
  *
  *     'hello'.match('(.)\1')      #=> #<MatchData "ll" 1:"l">
  *     'hello'.match('(.)\1')[0]   #=> "ll"
@@ -2586,7 +2589,7 @@
 {
     VALUE re, result;
     if (argc < 1)
-	rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
+	rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..2)", argc);
     re = argv[0];
     argv[0] = str;
     result = rb_funcall2(get_pat(re, 0), rb_intern("match"), argc, argv);

In This Thread

Prev Next