Project

General

Profile

Bug #11130

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

 須藤です。 

 ~~~diff 
 
 ~~~ 
 +      if (NIL_P(size) || size == Qundef) { 
 + 	 ary = rb_ary_new(); 
 +      } 
 +      else { 
 + 	 ary = rb_ary_new_capa(NUM2LONG(size)); 
 +      } 

 ~~~ 
  
 

 ~~~c 
     
 
 ~~~ 
      if (FIXNUM_P(size)) { 
	 
 	 ary = rb_ary_new_capa(NUM2LONG(size)); 
     
      } 
     
      else { 
	 
 	 ary = rb_ary_new(); 
     
      } 

 ~~~ 
  
  とかsizeが返す値が数値じゃなかったらこれまでと同じ挙動にする 
 
  ようにしてもらえないでしょうか? 

 
 
  これまでは 

 ~~~ruby 
 
 
 ~~~ 
  class NonIntegerSizeEnum 
   
    include Enumerable 

   
 
    def initialize(n) 
     
      @n = n 
   
    end 

   
 
    def each 
     
      @n.times { |i| yield i } 
   
    end 

   
 
    def size 
     
      :size 
   
    end 
 
  end 

 

  NonIntegerSizeEnum.new(100).to_a 
 ~~~ 

  
 
  というコードが動いていたんですが、この変更の後からは 

 
 
 > /tmp/b.rb:17:in `to_a': no implicit conversion of Symbol into Integer (TypeError) 

 

  というエラーがでるようになってしまって困っています。 
 
 > ~~~ 
 > In <20150510022540.1547A118611@sakura2.atdot.net> 
 > "[ruby-changes:38376] glass:r50457 (trunk): * enum.c (enum_to_a): Use size to set array capa when possible." on Sun, 10 May 2015 11:25:40 +0900 (JST), 
 > glass <ko1@atdot.net> wrote: 
 >  
 > > glass 	 2015-05-10 11:25:33 +0900 (Sun, 10 May 2015) 
 > >  
 > >     New Revision: 50457 
 > >  
 > >     http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50457 
 > >  
 > >     Log: 
 > >       * enum.c (enum_to_a): Use size to set array capa when possible. 
 > >         the patch is from HonoreDB <aweiner at mdsol.com>. 
 > >         [fix GH-444] 
 > >  
 > >     Modified files: 
 > >       trunk/ChangeLog 
 > >       trunk/enum.c 
 > > Index: ChangeLog 
 > > =================================================================== 
 > > --- ChangeLog 	 (revision 50456) 
 > > +++ ChangeLog 	 (revision 50457) 
 > > @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 
 > > +Sun May 10 11:23:03 2015    Masaki Matsushita <glass.saga@gmail.com> 
 > > + 
 > > + 	 * enum.c (enum_to_a): Use size to set array capa when possible. 
 > > + 	   the patch is from HonoreDB <aweiner at mdsol.com>. 
 > > + 	   [fix GH-444] 
 > > + 
 > >    Sat May    9 06:48:36 2015    Eric Wong    <e@80x24.org> 
 > >   
 > >   	 * ext/socket/ancdata.c (bsock_recvmsg_internal): GC guard 
 > > Index: enum.c 
 > > =================================================================== 
 > > --- enum.c 	 (revision 50456) 
 > > +++ enum.c 	 (revision 50457) 
 > > @@ -515,7 +515,14 @@ enum_flat_map(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enum.c#L515 
 > >    static VALUE 
 > >    enum_to_a(int argc, VALUE *argv, VALUE obj) 
 > >    { 
 > > -      VALUE ary = rb_ary_new(); 
 > > +      VALUE ary, size = rb_check_funcall(obj, id_size, 0, 0); 
 > > + 
 > > +      if (NIL_P(size) || size == Qundef) { 
 > > + 	 ary = rb_ary_new(); 
 > > +      } 
 > > +      else { 
 > > + 	 ary = rb_ary_new_capa(NUM2LONG(size)); 
 > > +      } 
 > >   
 > >        rb_block_call(obj, id_each, argc, argv, collect_all, ary); 
 > >        OBJ_INFECT(ary, obj); 
 > >  
 > > -- 
 > > ML: ruby-changes@quickml.atdot.net 
 > > Info: http://www.atdot.net/~ko1/quickml/ 
 > ~~~

Back