Ignore:
Timestamp:
Jul 9, 2020, 8:51:43 AM (4 years ago)
Author:
coas-nagasima
Message:

mrubyを2.1.1に更新

Location:
EcnlProtoTool/trunk/mruby-2.1.1
Files:
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enumerator/mrbgem.rake

    r321 r439  
    33  spec.author  = 'mruby developers'
    44  spec.add_dependency('mruby-fiber', :core => 'mruby-fiber')
    5   spec.add_dependency 'mruby-enum-ext', :core => 'mruby-enum-ext'
    65  spec.summary = 'Enumerator class'
    76end
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enumerator/mrblib/enumerator.rb

    r331 r439  
    9090
    9191  ##
    92   # @overload initialize(size = nil, &block)
    9392  # @overload initialize(obj, method = :each, *args)
    9493  #
     
    110109  #     p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
    111110  #
    112   def initialize(obj=nil, meth=:each, *args, &block)
    113     if block_given?
     111  # In the second, deprecated, form, a generated Enumerator iterates over the
     112  # given object using the given method with the given arguments passed. This
     113  # form is left only for internal use.
     114  #
     115  # Use of this form is discouraged.  Use Kernel#enum_for or Kernel#to_enum
     116  # instead.
     117  def initialize(obj=NONE, meth=:each, *args, &block)
     118    if block
    114119      obj = Generator.new(&block)
    115     else
    116       raise ArgumentError unless obj
     120    elsif obj == NONE
     121      raise ArgumentError, "wrong number of arguments (given 0, expected 1+)"
    117122    end
    118123
    119124    @obj = obj
    120125    @meth = meth
    121     @args = args.dup
     126    @args = args
    122127    @fib = nil
    123128    @dst = nil
     
    126131    @stop_exc = false
    127132  end
    128   attr_accessor :obj, :meth, :args, :fib
    129   private :obj, :meth, :args, :fib
     133  attr_accessor :obj, :meth, :args
     134  attr_reader :fib
    130135
    131136  def initialize_copy(obj)
     
    152157  # +offset+:: the starting index to use
    153158  #
    154   def with_index(offset=0)
    155     return to_enum :with_index, offset unless block_given?
    156     offset = if offset.nil?
    157       0
    158     elsif offset.respond_to?(:to_int)
    159       offset.to_int
     159  def with_index(offset=0, &block)
     160    return to_enum :with_index, offset unless block
     161
     162    if offset.nil?
     163      offset = 0
    160164    else
    161       raise TypeError, "no implicit conversion of #{offset.class} into Integer"
     165      offset = offset.__to_int
    162166    end
    163167
     
    165169    enumerator_block_call do |*i|
    166170      n += 1
    167       yield i.__svalue, n
     171      block.call i.__svalue, n
    168172    end
    169173  end
     
    210214  #   # => foo:2
    211215  #
    212   def with_object(object)
    213     return to_enum(:with_object, object) unless block_given?
     216  def with_object(object, &block)
     217    return to_enum(:with_object, object) unless block
    214218
    215219    enumerator_block_call do |i|
    216       yield [i,object]
     220      block.call [i,object]
    217221    end
    218222    object
     
    220224
    221225  def inspect
    222     return "#<#{self.class}: uninitialized>" unless @obj
    223 
    224226    if @args && @args.size > 0
    225227      args = @args.join(", ")
    226       "#<#{self.class}: #{@obj}:#{@meth}(#{args})>"
     228      "#<#{self.class}: #{@obj.inspect}:#{@meth}(#{args})>"
    227229    else
    228       "#<#{self.class}: #{@obj}:#{@meth}>"
     230      "#<#{self.class}: #{@obj.inspect}:#{@meth}>"
    229231    end
    230232  end
     
    242244  # === Examples
    243245  #
    244   #   "Hello, world!".scan(/\w+/)                     #=> ["Hello", "world"]
    245   #   "Hello, world!".to_enum(:scan, /\w+/).to_a      #=> ["Hello", "world"]
    246   #   "Hello, world!".to_enum(:scan).each(/\w+/).to_a #=> ["Hello", "world"]
     246  #   Array.new(3)                     #=> [nil, nil, nil]
     247  #   Array.new(3) { |i| i }           #=> [0, 1, 2]
     248  #   Array.to_enum(:new, 3).to_a      #=> [0, 1, 2]
     249  #   Array.to_enum(:new).each(3).to_a #=> [0, 1, 2]
    247250  #
    248251  #   obj = Object.new
     
    278281      obj.args = args
    279282    end
    280     return obj unless block_given?
     283    return obj unless block
    281284    enumerator_block_call(&block)
    282285  end
     
    539542  class Yielder
    540543    def initialize(&block)
    541       raise LocalJumpError, "no block given" unless block_given?
     544      raise LocalJumpError, "no block given" unless block
    542545
    543546      @proc = block
     
    553556    end
    554557  end
     558
     559  ##
     560  # call-seq:
     561  #    Enumerator.produce(initial = nil) { |val| } -> enumerator
     562  #
     563  # Creates an infinite enumerator from any block, just called over and
     564  # over.  Result of the previous iteration is passed to the next one.
     565  # If +initial+ is provided, it is passed to the first iteration, and
     566  # becomes the first element of the enumerator; if it is not provided,
     567  # first iteration receives +nil+, and its result becomes first
     568  # element of the iterator.
     569  #
     570  # Raising StopIteration from the block stops an iteration.
     571  #
     572  # Examples of usage:
     573  #
     574  #   Enumerator.produce(1, &:succ)   # => enumerator of 1, 2, 3, 4, ....
     575  #
     576  #   Enumerator.produce { rand(10) } # => infinite random number sequence
     577  #
     578  #   ancestors = Enumerator.produce(node) { |prev| node = prev.parent or raise StopIteration }
     579  #   enclosing_section = ancestors.find { |n| n.type == :section }
     580  def Enumerator.produce(init=NONE, &block)
     581    raise ArgumentError, "no block given" if block.nil?
     582    Enumerator.new do |y|
     583      if init == NONE
     584        val = nil
     585      else
     586        val = init
     587        y.yield(val)
     588      end
     589      begin
     590        while true
     591          y.yield(val = block.call(val))
     592        end
     593      rescue StopIteration
     594        # do nothing
     595      end
     596    end
     597  end
    555598end
    556599
     
    560603  #   obj.to_enum(method = :each, *args)                 -> enum
    561604  #   obj.enum_for(method = :each, *args)                -> enum
    562   #   obj.to_enum(method = :each, *args) {|*args| block} -> enum
    563   #   obj.enum_for(method = :each, *args){|*args| block} -> enum
    564605  #
    565606  # Creates a new Enumerator which will enumerate by calling +method+ on
    566607  # +obj+, passing +args+ if any.
    567   #
    568   # If a block is given, it will be used to calculate the size of
    569   # the enumerator without the need to iterate it (see Enumerator#size).
    570608  #
    571609  # === Examples
     
    586624  # a generic Enumerable, in case no block is passed.
    587625  #
    588   # Here is such an example, with parameter passing and a sizing block:
     626  # Here is such an example with parameter passing:
    589627  #
    590628  #     module Enumerable
     
    593631  #         raise ArgumentError, "#{n} is negative!" if n < 0
    594632  #         unless block_given?
    595   #           return to_enum(__method__, n) do # __method__ is :repeat here
    596   #             sz = size     # Call size and multiply by n...
    597   #             sz * n if sz  # but return nil if size itself is nil
    598   #           end
     633  #           return to_enum(__method__, n) # __method__ is :repeat here
    599634  #         end
    600635  #         each do |*val|
     
    613648    Enumerator.new self, meth, *args
    614649  end
    615   alias :enum_for :to_enum
     650  alias enum_for to_enum
    616651end
    617652
    618653module Enumerable
    619654  # use Enumerator to use infinite sequence
    620   def zip(*arg)
    621     ary = []
    622     arg = arg.map{|a|a.each}
    623     i = 0
    624     self.each do |*val|
    625       a = []
    626       a.push(val.__svalue)
    627       idx = 0
    628       while idx < arg.size
    629         begin
    630           a.push(arg[idx].next)
    631         rescue StopIteration
    632           a.push(nil)
     655  def zip(*args, &block)
     656    args = args.map do |a|
     657      if a.respond_to?(:each)
     658        a.to_enum(:each)
     659      else
     660        raise TypeError, "wrong argument type #{a.class} (must respond to :each)"
     661      end
     662    end
     663
     664    result = block ? nil : []
     665
     666    each do |*val|
     667      tmp = [val.__svalue]
     668      args.each do |arg|
     669        v = if arg.nil?
     670          nil
     671        else
     672          begin
     673            arg.next
     674          rescue StopIteration
     675            nil
     676          end
    633677        end
    634         idx += 1
     678        tmp.push(v)
    635679      end
    636       ary.push(a)
    637       i += 1
    638     end
    639     ary
     680      if result.nil?
     681        block.call(tmp)
     682      else
     683        result.push(tmp)
     684      end
     685    end
     686
     687    result
    640688  end
    641689end
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-enumerator/test/enumerator.rb

    r331 r439  
    77end
    88
    9 assert 'Enumerator' do
     9def assert_take(exp, enumerator)
     10  result = []
     11  n = exp.size
     12  enumerator.each do |v|
     13    result << v
     14    n -= 1
     15    break if n == 0
     16  end if n > 0
     17  assert_equal exp, result
     18end
     19
     20assert 'Enumerator.class' do
    1021  assert_equal Class, Enumerator.class
    1122end
    1223
    13 assert 'Enumerator' do
     24assert 'Enumerator.superclass' do
    1425  assert_equal Object, Enumerator.superclass
    1526end
     
    2031  assert_equal [[:x,1],[:y,2]], {x:1, y:2}.each.map{|i| i}.sort
    2132  assert_equal [1,2,3], @obj.to_enum(:foo, 1,2,3).to_a
    22   assert_equal [1,2,3], Enumerator.new(@obj, :foo, 1,2,3).to_a
    23   assert_equal [1,2,3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.take(3)
     33  assert_take [1,2,3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }
    2434  assert_raise(ArgumentError) { Enumerator.new }
    25   enum = @obj.to_enum
    26   assert_raise(NoMethodError) { enum.each {} }
    2735
    2836  # examples
     
    3442    end
    3543  end
    36   assert_equal fib.take(10), [1,1,2,3,5,8,13,21,34,55]
     44  assert_take [1,1,2,3,5,8,13,21,34,55], fib
    3745end
    3846
     
    5462  @obj.to_enum(:foo, 1, 2, 3).with_index(10).with_index(20) { |*i| a << i }
    5563  assert_equal [[[1, 10], 20], [[2, 11], 21], [[3, 12], 22]], a
    56 end
    57 
    58 assert 'Enumerator#with_index nonnum offset' do
    59   s = Object.new
    60   def s.to_int; 1 end
    61   assert_equal([[1,1],[2,2],[3,3]], @obj.to_enum(:foo, 1, 2, 3).with_index(s).to_a)
    6264end
    6365
     
    100102assert 'Enumerator#inspect' do
    101103  e = (0..10).each
    102   assert_equal("#<Enumerator: 0..10:each>", e.inspect)
    103   e = Enumerator.new("FooObject", :foo, 1)
    104   assert_equal("#<Enumerator: FooObject:foo(1)>", e.inspect)
    105   e = Enumerator.new("FooObject", :foo, 1, 2, 3)
    106   assert_equal("#<Enumerator: FooObject:foo(1, 2, 3)>", e.inspect)
     104  assert_equal('#<Enumerator: 0..10:each>', e.inspect)
     105  e = 'FooObject'.enum_for(:foo, 1)
     106  assert_equal('#<Enumerator: "FooObject":foo(1)>', e.inspect)
     107  e = 'FooObject'.enum_for(:foo, 1, 2, 3)
     108  assert_equal('#<Enumerator: "FooObject":foo(1, 2, 3)>', e.inspect)
     109  e = nil.enum_for(:to_s)
     110  assert_equal('#<Enumerator: nil:to_s>', e.inspect)
    107111end
    108112
     
    426430
    427431assert 'Kernel#to_enum' do
     432  e = nil
    428433  assert_equal Enumerator, [].to_enum.class
    429   assert_raise(ArgumentError){ nil.to_enum }
     434  assert_nothing_raised { e = [].to_enum(:_not_implemented_) }
     435  assert_raise(NoMethodError) { e.first }
    430436end
    431437
     
    510516assert 'Hash#select' do
    511517  h = {1=>2,3=>4,5=>6}
    512   hret = h.select.with_index {|a,b| a[1] == 4}
     518  hret = h.select.with_index {|a,_b| a[1] == 4}
    513519  assert_equal({3=>4}, hret)
    514520  assert_equal({1=>2,3=>4,5=>6}, h)
     
    517523assert 'Hash#select!' do
    518524  h = {1=>2,3=>4,5=>6}
    519   hret = h.select!.with_index {|a,b| a[1] == 4}
     525  hret = h.select!.with_index {|a,_b| a[1] == 4}
    520526  assert_equal h, hret
    521527  assert_equal({3=>4}, h)
     
    524530assert 'Hash#reject' do
    525531  h = {1=>2,3=>4,5=>6}
    526   hret = h.reject.with_index {|a,b| a[1] == 4}
     532  hret = h.reject.with_index {|a,_b| a[1] == 4}
    527533  assert_equal({1=>2,5=>6}, hret)
    528534  assert_equal({1=>2,3=>4,5=>6}, h)
     
    531537assert 'Hash#reject!' do
    532538  h = {1=>2,3=>4,5=>6}
    533   hret = h.reject!.with_index {|a,b| a[1] == 4}
     539  hret = h.reject!.with_index {|a,_b| a[1] == 4}
    534540  assert_equal h, hret
    535541  assert_equal({1=>2,5=>6}, h)
     
    545551  assert_equal [1,2,3,4,5], c
    546552end
     553
     554assert 'Enumerable#zip' do
     555  assert_equal [[1, 10], [2, 11], [3, 12]], [1,2,3].zip(10..Float::INFINITY)
     556
     557  ret = []
     558  assert_equal nil, [1,2,3].zip(10..Float::INFINITY) { |i| ret << i }
     559  assert_equal [[1, 10], [2, 11], [3, 12]], ret
     560
     561  assert_raise(TypeError) { [1].zip(1) }
     562end
     563
     564assert 'Enumerator.produce' do
     565  assert_raise(ArgumentError) { Enumerator.produce }
     566
     567  # Without initial object
     568  passed_args = []
     569  enum = Enumerator.produce {|obj| passed_args << obj; (obj || 0).succ }
     570  assert_equal Enumerator, enum.class
     571  assert_take [1, 2, 3], enum
     572  assert_equal [nil, 1, 2], passed_args
     573
     574  # With initial object
     575  passed_args = []
     576  enum = Enumerator.produce(1) {|obj| passed_args << obj; obj.succ }
     577  assert_take [1, 2, 3], enum
     578  assert_equal [1, 2], passed_args
     579
     580  # Raising StopIteration
     581  words = %w[The quick brown fox jumps over the lazy dog]
     582  enum = Enumerator.produce { words.shift or raise StopIteration }
     583  assert_equal %w[The quick brown fox jumps over the lazy dog], enum.to_a
     584
     585  # Raising StopIteration
     586  object = [[[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], "stuv", "wxyz"]
     587  enum = Enumerator.produce(object) {|obj|
     588    obj.respond_to?(:first) or raise StopIteration
     589    obj.first
     590  }
     591  assert_nothing_raised {
     592    assert_equal [
     593      [[[["abc", "def"], "ghi", "jkl"], "mno", "pqr"], "stuv", "wxyz"],
     594      [[["abc", "def"], "ghi", "jkl"], "mno", "pqr"],
     595      [["abc", "def"], "ghi", "jkl"],
     596      ["abc", "def"],
     597      "abc",
     598    ], enum.to_a
     599  }
     600end
Note: See TracChangeset for help on using the changeset viewer.