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/mrblib/hash.rb

    r331 r439  
    1313  def ==(hash)
    1414    return true if self.equal?(hash)
    15     begin
    16       hash = hash.to_hash
    17     rescue NoMethodError
     15    unless Hash === hash
    1816      return false
    1917    end
     
    3331  def eql?(hash)
    3432    return true if self.equal?(hash)
    35     begin
    36       hash = hash.to_hash
    37     rescue NoMethodError
     33    unless Hash === hash
    3834      return false
    3935    end
     
    5652  def delete(key, &block)
    5753    if block && !self.has_key?(key)
    58       block.call(key)
    59     else
    60       self.__delete(key)
    61     end
     54      return block.call(key)
     55    end
     56    self.__delete(key)
    6257  end
    6358
     
    8580  # ISO 15.2.13.4.9
    8681  def each(&block)
    87     return to_enum :each unless block_given?
     82    return to_enum :each unless block
    8883
    8984    keys = self.keys
     
    118113  # ISO 15.2.13.4.10
    119114  def each_key(&block)
    120     return to_enum :each_key unless block_given?
     115    return to_enum :each_key unless block
    121116
    122117    self.keys.each{|k| block.call(k)}
     
    144139  # ISO 15.2.13.4.11
    145140  def each_value(&block)
    146     return to_enum :each_value unless block_given?
     141    return to_enum :each_value unless block
    147142
    148143    self.keys.each{|k| block.call(self[k])}
     
    155150  # ISO 15.2.13.4.23
    156151  def replace(hash)
    157     raise TypeError, "can't convert argument into Hash" unless hash.respond_to?(:to_hash)
     152    raise TypeError, "Hash required (#{hash.class} given)" unless Hash === hash
    158153    self.clear
    159     hash = hash.to_hash
    160154    hash.each_key{|k|
    161155      self[k] = hash[k]
     
    180174  # ISO 15.2.13.4.22
    181175  def merge(other, &block)
    182     h = {}
    183     raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
    184     other = other.to_hash
    185     self.each_key{|k| h[k] = self[k]}
     176    raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
     177    h = self.dup
    186178    if block
    187179      other.each_key{|k|
     
    195187
    196188  # internal method for Hash inspection
    197   def _inspect
     189  def _inspect(recur_list)
    198190    return "{}" if self.size == 0
    199     "{"+self.map {|k,v|
    200       k._inspect + "=>" + v._inspect
    201     }.join(", ")+"}"
     191    return "{...}" if recur_list[self.object_id]
     192    recur_list[self.object_id] = true
     193    ary=[]
     194    keys=self.keys
     195    size=keys.size
     196    i=0
     197    while i<size
     198      k=keys[i]
     199      ary<<(k._inspect(recur_list) + "=>" + self[k]._inspect(recur_list))
     200      i+=1
     201    end
     202    "{"+ary.join(", ")+"}"
    202203  end
    203204  ##
     
    206207  # ISO 15.2.13.4.30 (x)
    207208  def inspect
    208     begin
    209       self._inspect
    210     rescue SystemStackError
    211       "{...}"
    212     end
     209    self._inspect({})
    213210  end
    214211  # ISO 15.2.13.4.31 (x)
     
    225222  #  1.8/1.9 Hash#reject! returns Hash; ISO says nothing.
    226223  #
    227   def reject!(&b)
    228     return to_enum :reject! unless block_given?
     224  def reject!(&block)
     225    return to_enum :reject! unless block
    229226
    230227    keys = []
    231228    self.each{|k,v|
    232       if b.call([k, v])
     229      if block.call([k, v])
    233230        keys.push(k)
    234231      end
     
    256253  #  1.8/1.9 Hash#reject returns Hash; ISO says nothing.
    257254  #
    258   def reject(&b)
    259     return to_enum :reject unless block_given?
     255  def reject(&block)
     256    return to_enum :reject unless block
    260257
    261258    h = {}
    262259    self.each{|k,v|
    263       unless b.call([k, v])
     260      unless block.call([k, v])
    264261        h[k] = v
    265262      end
     
    278275  #  1.9 Hash#select! returns Hash; ISO says nothing.
    279276  #
    280   def select!(&b)
    281     return to_enum :select! unless block_given?
     277  def select!(&block)
     278    return to_enum :select! unless block
    282279
    283280    keys = []
    284281    self.each{|k,v|
    285       unless b.call([k, v])
     282      unless block.call([k, v])
    286283        keys.push(k)
    287284      end
     
    309306  #  1.9 Hash#select returns Hash; ISO says nothing
    310307  #
    311   def select(&b)
    312     return to_enum :select unless block_given?
     308  def select(&block)
     309    return to_enum :select unless block
    313310
    314311    h = {}
    315312    self.each{|k,v|
    316       if b.call([k, v])
     313      if block.call([k, v])
    317314        h[k] = v
    318315      end
    319316    }
    320317    h
    321   end
    322 
    323   ##
    324   #  call-seq:
    325   #    hsh.rehash -> hsh
    326   #
    327   #  Rebuilds the hash based on the current hash values for each key. If
    328   #  values of key objects have changed since they were inserted, this
    329   #  method will reindex <i>hsh</i>.
    330   #
    331   #     h = {"AAA" => "b"}
    332   #     h.keys[0].chop!
    333   #     h          #=> {"AA"=>"b"}
    334   #     h["AA"]    #=> nil
    335   #     h.rehash   #=> {"AA"=>"b"}
    336   #     h["AA"]    #=> "b"
    337   #
    338   def rehash
    339     h = {}
    340     self.each{|k,v|
    341       h[k] = v
    342     }
    343     self.replace(h)
    344   end
    345 
    346   def __update(h)
    347     h.each_key{|k| self[k] = h[k]}
    348     self
    349318  end
    350319end
Note: See TracChangeset for help on using the changeset viewer.