Changeset 331 for EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-hash-ext
- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-1.3.0
- Files:
-
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-hash-ext/mrblib/hash.rb
r321 r331 29 29 o = object[0] 30 30 if o.respond_to?(:to_hash) 31 h = Hash.new31 h = self.new 32 32 object[0].to_hash.each { |k, v| h[k] = v } 33 33 return h 34 34 elsif o.respond_to?(:to_a) 35 h = Hash.new35 h = self.new 36 36 o.to_a.each do |i| 37 37 raise ArgumentError, "wrong element type #{i.class} (expected array)" unless i.respond_to?(:to_a) … … 54 54 raise ArgumentError, 'odd number of arguments for Hash' 55 55 end 56 h = Hash.new56 h = self.new 57 57 0.step(length - 2, 2) do |i| 58 58 h[object[i]] = object[i + 1] 59 59 end 60 60 h 61 end 62 63 ## 64 # 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 61 80 end 62 81 … … 193 212 194 213 def invert 195 h = Hash.new214 h = self.class.new 196 215 self.each {|k, v| h[v] = k } 197 216 h … … 347 366 } 348 367 end 368 369 ## 370 # call-seq: 371 # hsh.dig(key,...) -> object 372 # 373 # Extracts the nested value specified by the sequence of <i>key</i> 374 # objects by calling +dig+ at each step, returning +nil+ if any 375 # intermediate step is +nil+. 376 # 377 def dig(idx,*args) 378 n = self[idx] 379 if args.size > 0 380 n&.dig(*args) 381 else 382 n 383 end 384 end 349 385 end -
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-hash-ext/src/hash-ext.c
r321 r331 5 5 */ 6 6 7 #include "mruby.h"8 #include "mruby/array.h"9 #include "mruby/hash.h"7 #include <mruby.h> 8 #include <mruby/array.h> 9 #include <mruby/hash.h> 10 10 11 11 /* -
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-hash-ext/test/hash.rb
r321 r331 38 38 Hash['d_key'] 39 39 end 40 end 41 42 assert('Hash.[] for sub class') do 43 sub_hash_class = Class.new(Hash) 44 sub_hash = sub_hash_class[] 45 assert_equal(sub_hash_class, sub_hash.class) 46 end 47 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})) 40 52 end 41 53 … … 138 150 end 139 151 152 assert("Hash#invert with sub class") do 153 sub_hash_class = Class.new(Hash) 154 sub_hash = sub_hash_class.new 155 assert_equal(sub_hash_class, sub_hash.invert.class) 156 end 157 140 158 assert("Hash#keep_if") do 141 159 h = { 1 => 2, 3 => 4, 5 => 6 } … … 231 249 assert_false(h2 > h2) 232 250 end 251 252 assert("Hash#dig") do 253 h = {a:{b:{c:1}}} 254 assert_equal(1, h.dig(:a, :b, :c)) 255 assert_nil(h.dig(:d)) 256 end
Note:
See TracChangeset
for help on using the changeset viewer.