Skip to content

Commit

Permalink
stringio.c: warn block for new
Browse files Browse the repository at this point in the history
* ext/stringio/stringio.c (strio_s_new): warn if a block is given,
  as well as IO.new.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54792 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Apr 27, 2016
1 parent d226ce8 commit 7a5b566
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Wed Apr 27 15:47:54 2016 Nobuyoshi Nakada <[email protected]>

* ext/stringio/stringio.c (strio_s_new): warn if a block is given,
as well as IO.new.

Wed Apr 27 14:29:47 2016 Nobuyoshi Nakada <[email protected]>

* error.c (ruby_only_for_internal_use): raise fatal error when
Expand Down
14 changes: 14 additions & 0 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ strio_s_open(int argc, VALUE *argv, VALUE klass)
return rb_ensure(rb_yield, obj, strio_finalize, obj);
}

/* :nodoc: */
static VALUE
strio_s_new(int argc, VALUE *argv, VALUE klass)
{
if (rb_block_given_p()) {
VALUE cname = rb_obj_as_string(klass);

rb_warn("%"PRIsVALUE"::new() does not take block; use %"PRIsVALUE"::open() instead",
cname, cname);
}
return rb_class_new_instance(argc, argv, klass);
}

/*
* Returns +false+. Just for compatibility to IO.
*/
Expand Down Expand Up @@ -1523,6 +1536,7 @@ Init_stringio(void)

rb_include_module(StringIO, rb_mEnumerable);
rb_define_alloc_func(StringIO, strio_s_allocate);
rb_define_singleton_method(StringIO, "new", strio_s_new, -1);
rb_define_singleton_method(StringIO, "open", strio_s_open, -1);
rb_define_method(StringIO, "initialize", strio_initialize, -1);
rb_define_method(StringIO, "initialize_copy", strio_copy, 1);
Expand Down
6 changes: 6 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,10 @@ def test_binmode
bug_11945 = '[ruby-core:72699] [Bug #11945]'
assert_equal Encoding::ASCII_8BIT, s.external_encoding, bug_11945
end

def test_new_block_warning
assert_warn(/does not take block/) do
StringIO.new {}
end
end
end

0 comments on commit 7a5b566

Please sign in to comment.