1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* io.c (rb_io_s_foreach): argument check before making Enumerator.

[ruby-dev:31525]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34736 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-02-22 08:36:25 +00:00
parent 873b56fd50
commit 31d8a97657
3 changed files with 14 additions and 1 deletions

View file

@ -1,3 +1,8 @@
Wed Feb 22 17:36:22 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c (rb_io_s_foreach): argument check before making Enumerator.
[ruby-dev:31525]
Wed Feb 22 17:07:35 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c (rb_io_s_foreach): return enumerator including kerword

3
io.c
View file

@ -8969,10 +8969,11 @@ static VALUE
rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
{
VALUE opt;
int orig_argc = argc;
struct foreach_arg arg;
RETURN_ENUMERATOR(self, argc, argv);
argc = rb_scan_args(argc, argv, "13:", NULL, NULL, NULL, NULL, &opt);
RETURN_ENUMERATOR(self, orig_argc, argv);
open_key_args(argc, argv, opt, &arg);
if (NIL_P(arg.io)) return Qnil;
return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, arg.io);

View file

@ -1605,6 +1605,13 @@ End
IO.foreach(t.path, "b", 3) {|x| a << x }
assert_equal(["foo", "\nb", "ar\n", "b", "az\n"], a)
bug = '[ruby-dev:31525]'
assert_raise(ArgumentError, bug) {IO.foreach}
a = nil
assert_nothing_raised(ArgumentError, bug) {a = IO.foreach(t.path).to_a}
assert_equal(["foo\n", "bar\n", "baz\n"], a, bug)
bug6054 = '[ruby-dev:45267]'
e = assert_raise(IOError, bug6054) {IO.foreach(t.path, mode:"w").next}
assert_match(/not opened for reading/, e.message, bug6054)