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:
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.