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