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

Legend:

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

    r321 r439  
    33  spec.author  = 'mruby developers'
    44  spec.summary = 'Hash class extension'
    5   spec.add_dependency 'mruby-enum-ext', :core => 'mruby-enum-ext'
    6   spec.add_dependency 'mruby-array-ext', :core => 'mruby-array-ext'
     5  spec.add_dependency 'mruby-array-ext', core: 'mruby-array-ext'
    76end
  • 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
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-hash-ext/src/hash-ext.c

    r331 r439  
    3737}
    3838
     39/*
     40 *  call-seq:
     41 *     hsh.slice(*keys) -> a_hash
     42 *
     43 *  Returns a hash containing only the given keys and their values.
     44 *
     45 *     h = { a: 100, b: 200, c: 300 }
     46 *     h.slice(:a)           #=> {:a=>100}
     47 *     h.slice(:b, :c, :d)   #=> {:b=>200, :c=>300}
     48 */
     49static mrb_value
     50hash_slice(mrb_state *mrb, mrb_value hash)
     51{
     52  mrb_value *argv, result;
     53  mrb_int argc, i;
     54
     55  mrb_get_args(mrb, "*", &argv, &argc);
     56  result = mrb_hash_new_capa(mrb, argc);
     57  if (argc == 0) return result; /* empty hash */
     58  for (i = 0; i < argc; i++) {
     59    mrb_value key = argv[i];
     60    mrb_value val;
     61
     62    val = mrb_hash_fetch(mrb, hash, key, mrb_undef_value());
     63    if (!mrb_undef_p(val)) {
     64      mrb_hash_set(mrb, result, key, val);
     65    }
     66  }
     67  return result;
     68}
     69
    3970void
    4071mrb_mruby_hash_ext_gem_init(mrb_state *mrb)
     
    4475  h = mrb->hash_class;
    4576  mrb_define_method(mrb, h, "values_at", hash_values_at, MRB_ARGS_ANY());
     77  mrb_define_method(mrb, h, "slice",     hash_slice, MRB_ARGS_ANY());
    4678}
    4779
  • EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-hash-ext/test/hash.rb

    r331 r439  
    4646end
    4747
    48 assert('Hash.try_convert') do
    49   assert_nil Hash.try_convert(nil)
    50   assert_nil Hash.try_convert("{1=>2}")
    51   assert_equal({1=>2}, Hash.try_convert({1=>2}))
    52 end
    53 
    5448assert('Hash#merge!') do
    5549  a = { 'abc_key' => 'abc_value', 'cba_key' => 'cba_value' }
     
    8175  h = Hash.new { |hash,k| hash[k] = k }
    8276  assert_equal keys, h.values_at(*keys)
     77end
     78
     79assert('Hash#compact') do
     80  h = { "cat" => "feline", "dog" => nil, "cow" => false }
     81
     82  assert_equal({ "cat" => "feline", "cow" => false }, h.compact)
     83  assert_equal({ "cat" => "feline", "dog" => nil, "cow" => false }, h)
     84end
     85
     86assert('Hash#compact!') do
     87  h = { "cat" => "feline", "dog" => nil, "cow" => false }
     88
     89  h.compact!
     90  assert_equal({ "cat" => "feline", "cow" => false }, h)
    8391end
    8492
     
    255263  assert_nil(h.dig(:d))
    256264end
     265
     266assert("Hash#transform_keys") do
     267  h = {"1" => 100, "2" => 200}
     268  assert_equal({"1!" => 100, "2!" => 200},
     269               h.transform_keys{|k| k+"!"})
     270  assert_equal({1 => 100, 2 => 200},
     271               h.transform_keys{|k|k.to_i})
     272  assert_same(h, h.transform_keys!{|k|k.to_i})
     273  assert_equal({1 => 100, 2 => 200}, h)
     274end
     275
     276assert("Hash#transform_values") do
     277  h = {a: 1, b: 2, c: 3}
     278  assert_equal({a: 2, b: 5, c: 10},
     279               h.transform_values{|v| v * v + 1})
     280  assert_equal({a: "1", b: "2", c: "3"},
     281               h.transform_values{|v|v.to_s})
     282  assert_same(h, h.transform_values!{|v|v.to_s})
     283  assert_equal({a: "1", b: "2", c: "3"}, h)
     284end
     285
     286assert("Hash#slice") do
     287  h = { a: 100, b: 200, c: 300 }
     288  assert_equal({:a=>100}, h.slice(:a))
     289  assert_equal({:b=>200, :c=>300}, h.slice(:b, :c, :d))
     290end
Note: See TracChangeset for help on using the changeset viewer.