Class: Hash
- Includes:
- RMTools::KeyValueTraversal
- Defined in:
- lib/rmtools/core/js.rb,
lib/rmtools/dev/present.rb,
lib/rmtools/enumerable/hash.rb
Overview
Javascript hash getter/setter and string concat logic
Direct Known Subclasses
Instance Method Summary collapse
- #+(other_hash) ⇒ Object
- #any?(&block) ⇒ Boolean
- #id ⇒ Object
-
#map! ⇒ Object
Hashes map methods that doesn’t make hash into array.
-
#map2 ⇒ Object
Hashes map methods that doesn’t make hash into array.
-
#map_keys ⇒ Object
Hashes map methods that doesn’t make hash into array New hash may get shorter than source.
-
#map_keys! ⇒ Object
Hashes map methods that doesn’t make hash into array New hash may get shorter than source.
-
#map_values ⇒ Object
Hashes map methods that doesn’t make hash into array.
-
#map_values! ⇒ Object
Hashes map methods that doesn’t make hash into array.
- #max_by_key ⇒ Object
- #max_by_value ⇒ Object
-
#method_missing(method, *args) ⇒ Object
hash = {} hash.abc = 123 hash # => “abc”=>123 hash.abc # => 123 hash.unknown_function(321) # => raise NoMethodError hash.unknown_function # => nil hash = 456 hash.def # => 456.
- #min_by_key ⇒ Object
- #min_by_value ⇒ Object
- #present(inspect_string = nil) ⇒ Object
- #throw_no ⇒ Object
- #to_traversable ⇒ Object
- #truth_map ⇒ Object
-
#type ⇒ Object
Redefine since these methods are deprecated anyway.
- #unify_keys ⇒ Object
Methods included from RMTools::KeyValueTraversal
#depth_first_find, #depth_first_select, #depth_first_traverse, #preorder_find, #preorder_select, #preorder_traverse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
hash = {} hash.abc = 123 hash # => “abc”=>123 hash.abc # => 123 hash.unknown_function(321) # => raise NoMethodError hash.unknown_function # => nil hash = 456 hash.def # => 456
This priority should be stable, because
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rmtools/core/js.rb', line 17 def method_missing(method, *args) str = method.to_s if str =~ /=$/ self[str[0..-2]] = args[0] elsif !args.empty? or str =~ /[!?]$/ throw_no method else a = self[method] (a == default) ? self[str] : a end end |
Instance Method Details
#+(other_hash) ⇒ Object
14 15 16 |
# File 'lib/rmtools/enumerable/hash.rb', line 14 def +(other_hash) merge(other_hash || {}) end |
#any?(&block) ⇒ Boolean
48 49 50 |
# File 'lib/rmtools/enumerable/hash.rb', line 48 def any?(&block) !!find(&block) end |
#id ⇒ Object
34 35 36 37 |
# File 'lib/rmtools/core/js.rb', line 34 def id a = self[:id] (a == default) ? self['id'] : a end |
#map! ⇒ Object
Hashes map methods that doesn’t make hash into array
373 374 375 376 |
# File 'ext/rmtools.cpp', line 373 def map! each {|k, v| self[k] = yield(k,v)} self end |
#map2 ⇒ Object
Hashes map methods that doesn’t make hash into array
62 63 64 65 66 |
# File 'lib/rmtools/enumerable/hash.rb', line 62 def map2 h = {} each {|k, v| h[k] = yield(k,v)} h end |
#map_keys ⇒ Object
Hashes map methods that doesn’t make hash into array New hash may get shorter than source
393 394 395 396 397 398 |
# File 'ext/rmtools.cpp', line 393 static VALUE rb_hash_map_keys(VALUE hash) { VALUE new_hash = rb_hash_new(); rb_hash_foreach(hash, (int (*)(ANYARGS))map_keys_i, new_hash); return new_hash; } |
#map_keys! ⇒ Object
Hashes map methods that doesn’t make hash into array New hash may get shorter than source
355 356 357 358 359 |
# File 'ext/rmtools.cpp', line 355 static VALUE rb_hash_map_keys_bang(VALUE hash) { rb_hash_foreach(hash, (int (*)(ANYARGS))replace_keys_i, hash); return hash; } |
#map_values ⇒ Object
Hashes map methods that doesn’t make hash into array
382 383 384 385 386 387 |
# File 'ext/rmtools.cpp', line 382 static VALUE rb_hash_map_values(VALUE hash) { VALUE new_hash = rb_hash_new(); rb_hash_foreach(hash, (int (*)(ANYARGS))map_values_i, new_hash); return new_hash; } |
#map_values! ⇒ Object
Hashes map methods that doesn’t make hash into array
364 365 366 367 368 |
# File 'ext/rmtools.cpp', line 364 static VALUE rb_hash_map_values_bang(VALUE hash) { rb_hash_foreach(hash, (int (*)(ANYARGS))map_values_i, hash); return hash; } |
#max_by_key ⇒ Object
53 |
# File 'lib/rmtools/enumerable/hash.rb', line 53 def max_by_key; [(m = keys.max), self[m]] end |
#max_by_value ⇒ Object
57 |
# File 'lib/rmtools/enumerable/hash.rb', line 57 def max_by_value; [(m = values.max), self[m]] end |
#min_by_key ⇒ Object
55 |
# File 'lib/rmtools/enumerable/hash.rb', line 55 def min_by_key; [(m = keys.min), self[m]] end |
#min_by_value ⇒ Object
59 |
# File 'lib/rmtools/enumerable/hash.rb', line 59 def min_by_value; [(m = values.min), self[m]] end |
#present(inspect_string = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/rmtools/dev/present.rb', line 51 def present(inspect_string=nil) str = "{ " sorted = sort rescue to_a.sort_by_to_s str << sorted.map {|k,v| "#{RMTools::Painter.w((k.is String and !inspect_string) ? k : k.inspect)} => #{(v.is String and !inspect_string) ? v : v.inspect}," }*"\n " str << "}" puts str end |
#throw_no ⇒ Object
5 |
# File 'lib/rmtools/core/js.rb', line 5 alias :throw_no :method_missing |
#to_traversable ⇒ Object
10 11 12 |
# File 'lib/rmtools/enumerable/hash.rb', line 10 def to_traversable RMTools::KeyValueTraversable.new(self) end |
#truth_map ⇒ Object
73 74 75 |
# File 'lib/rmtools/enumerable/hash.rb', line 73 def truth_map map_values {|v| !!v} end |
#type ⇒ Object
Redefine since these methods are deprecated anyway
30 31 32 33 |
# File 'lib/rmtools/core/js.rb', line 30 def type a = self[:type] (a == default) ? self['type'] : a end |
#unify_keys ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rmtools/enumerable/hash.rb', line 19 def unify_keys keys.each {|k| case k when String sk = k.to_sym self[sk] = self[k] if !self[sk] when Symbol, Numeric sk = k.to_s self[sk] = self[k] if !self[sk] end } self end |