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

    r331 r439  
    2828    if length == 1
    2929      o = object[0]
    30       if o.respond_to?(:to_hash)
     30      if Hash === o
    3131        h = self.new
    32         object[0].to_hash.each { |k, v| h[k] = v }
     32        o.each { |k, v| h[k] = v }
    3333        return h
    3434      elsif o.respond_to?(:to_a)
     
    6363  ##
    6464  # call-seq:
    65   #     Hash.try_convert(obj) -> hash or nil
    66   #
    67   # Try to convert <i>obj</i> into a hash, using to_hash method.
    68   # Returns converted hash or nil if <i>obj</i> cannot be converted
    69   # for any reason.
    70   #
    71   #     Hash.try_convert({1=>2})   # => {1=>2}
    72   #     Hash.try_convert("1=>2")   # => nil
    73   #
    74   def self.try_convert(obj)
    75     if obj.respond_to?(:to_hash)
    76       obj.to_hash
    77     else
    78       nil
    79     end
    80   end
    81 
    82   ##
    83   # call-seq:
    8465  #     hsh.merge!(other_hash)                                 -> hsh
    8566  #     hsh.merge!(other_hash){|key, oldval, newval| block}    -> hsh
     
    10283
    10384  def merge!(other, &block)
    104     raise TypeError, "can't convert argument into Hash" unless other.respond_to?(:to_hash)
     85    raise TypeError, "Hash required (#{other.class} given)" unless Hash === other
    10586    if block
    10687      other.each_key{|k|
     
    11495
    11596  alias update merge!
     97
     98  ##
     99  # call-seq:
     100  #   hsh.compact!    -> hsh
     101  #
     102  # Removes all nil values from the hash. Returns the hash.
     103  # Returns nil if the hash does not contain nil values.
     104  #
     105  #   h = { a: 1, b: false, c: nil }
     106  #   h.compact!     #=> { a: 1, b: false }
     107  #
     108
     109  def compact!
     110    keys = self.keys
     111    nk = keys.select{|k|
     112      self[k] != nil
     113    }
     114    return nil if (keys.size == nk.size)
     115    h = {}
     116    nk.each {|k|
     117      h[k] = self[k]
     118    }
     119    h
     120    self.replace(h)
     121  end
     122
     123  ##
     124  # call-seq:
     125  #    hsh.compact     -> new_hsh
     126  #
     127  # Returns a new hash with the nil values/key pairs removed
     128  #
     129  #    h = { a: 1, b: false, c: nil }
     130  #    h.compact     #=> { a: 1, b: false }
     131  #    h             #=> { a: 1, b: false, c: nil }
     132  #
     133  def compact
     134    h = {}
     135    self.keys.select{|k|
     136      self[k] != nil
     137    }.each {|k|
     138      h[k] = self[k]
     139    }
     140    h
     141  end
    116142
    117143  ##
     
    150176        none
    151177      else
    152         raise KeyError, "Key not found: #{key}"
     178        raise KeyError, "Key not found: #{key.inspect}"
    153179      end
    154180    else
     
    172198
    173199  def delete_if(&block)
    174     return to_enum :delete_if unless block_given?
     200    return to_enum :delete_if unless block
    175201
    176202    self.each do |k, v|
     
    229255
    230256  def keep_if(&block)
    231     return to_enum :keep_if unless block_given?
     257    return to_enum :keep_if unless block
    232258
    233259    keys = []
     
    285311  #
    286312  def <(hash)
    287     begin
    288       hash = hash.to_hash
    289     rescue NoMethodError
    290       raise TypeError, "can't convert #{hash.class} to Hash"
    291     end
     313    raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
    292314    size < hash.size and all? {|key, val|
    293315      hash.key?(key) and hash[key] == val
     
    309331  #
    310332  def <=(hash)
    311     begin
    312       hash = hash.to_hash
    313     rescue NoMethodError
    314       raise TypeError, "can't convert #{hash.class} to Hash"
    315     end
     333    raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
    316334    size <= hash.size and all? {|key, val|
    317335      hash.key?(key) and hash[key] == val
     
    333351  #
    334352  def >(hash)
    335     begin
    336       hash = hash.to_hash
    337     rescue NoMethodError
    338       raise TypeError, "can't convert #{hash.class} to Hash"
    339     end
     353    raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
    340354    size > hash.size and hash.all? {|key, val|
    341355      key?(key) and self[key] == val
     
    357371  #
    358372  def >=(hash)
    359     begin
    360       hash = hash.to_hash
    361     rescue NoMethodError
    362       raise TypeError, "can't convert #{hash.class} to Hash"
    363     end
     373    raise TypeError, "can't convert #{hash.class} to Hash" unless Hash === hash
    364374    size >= hash.size and hash.all? {|key, val|
    365375      key?(key) and self[key] == val
     
    383393    end
    384394  end
     395
     396  ##
     397  # call-seq:
     398  #    hsh.transform_keys {|key| block } -> new_hash
     399  #    hsh.transform_keys                -> an_enumerator
     400  #
     401  # Returns a new hash, with the keys computed from running the block
     402  # once for each key in the hash, and the values unchanged.
     403  #
     404  # If no block is given, an enumerator is returned instead.
     405  #
     406  def transform_keys(&block)
     407    return to_enum :transform_keys unless block
     408    hash = {}
     409    self.keys.each do |k|
     410      new_key = block.call(k)
     411      hash[new_key] = self[k]
     412    end
     413    hash
     414  end
     415  ##
     416  # call-seq:
     417  #    hsh.transform_keys! {|key| block } -> hsh
     418  #    hsh.transform_keys!                -> an_enumerator
     419  #
     420  # Invokes the given block once for each key in <i>hsh</i>, replacing it
     421  # with the new key returned by the block, and then returns <i>hsh</i>.
     422  #
     423  # If no block is given, an enumerator is returned instead.
     424  #
     425  def transform_keys!(&block)
     426    return to_enum :transform_keys! unless block
     427    self.keys.each do |k|
     428      value = self[k]
     429      self.__delete(k)
     430      k = block.call(k) if block
     431      self[k] = value
     432    end
     433    self
     434  end
     435  ##
     436  # call-seq:
     437  #    hsh.transform_values {|value| block } -> new_hash
     438  #    hsh.transform_values                  -> an_enumerator
     439  #
     440  # Returns a new hash with the results of running the block once for
     441  # every value.
     442  # This method does not change the keys.
     443  #
     444  # If no block is given, an enumerator is returned instead.
     445  #
     446  def transform_values(&b)
     447    return to_enum :transform_values unless block_given?
     448    hash = {}
     449    self.keys.each do |k|
     450      hash[k] = yield(self[k])
     451    end
     452    hash
     453  end
     454
     455  ##
     456  # call-seq:
     457  #    hsh.transform_values! {|key| block } -> hsh
     458  #    hsh.transform_values!                -> an_enumerator
     459  #
     460  # Invokes the given block once for each value in the hash, replacing
     461  # with the new value returned by the block, and then returns <i>hsh</i>.
     462  #
     463  # If no block is given, an enumerator is returned instead.
     464  #
     465  def transform_values!(&b)
     466    return to_enum :transform_values! unless block_given?
     467    self.keys.each do |k|
     468      self[k] = yield(self[k])
     469    end
     470    self
     471  end
     472
     473  def to_proc
     474    ->x{self[x]}
     475  end
     476
     477  ##
     478  # call-seq:
     479  #   hsh.fetch_values(key, ...)                 -> array
     480  #   hsh.fetch_values(key, ...) { |key| block } -> array
     481  #
     482  # Returns an array containing the values associated with the given keys
     483  # but also raises <code>KeyError</code> when one of keys can't be found.
     484  # Also see <code>Hash#values_at</code> and <code>Hash#fetch</code>.
     485  #
     486  #   h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
     487  #
     488  #   h.fetch_values("cow", "cat")                   #=> ["bovine", "feline"]
     489  #   h.fetch_values("cow", "bird")                  # raises KeyError
     490  #   h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]
     491  #
     492  def fetch_values(*keys, &block)
     493    keys.map do |k|
     494      self.fetch(k, &block)
     495    end
     496  end
     497
     498  alias filter select
     499  alias filter! select!
    385500end
Note: See TracChangeset for help on using the changeset viewer.