- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-1.3.0
- Files:
-
- 1 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
Note:
See TracChangeset
for help on using the changeset viewer.