lib/rbx/hash.fy in fancy-0.8.0 vs lib/rbx/hash.fy in fancy-0.9.0
- old
+ new
@@ -5,10 +5,21 @@
alias_method: 'at:put: for: "[]="
alias_method: '[]: for: "[]="
alias_method: 'at: for: '[]
ruby_alias: '==
+ def initialize: default {
+ """
+ @default Default value for @self.
+
+ Initializes a new @Hash@ with @default as a default value.
+ If @default is a @Block@, call it with @self and a given key to get its default value.
+ """
+
+ default: default
+ }
+
def each: block {
each(&block)
}
def each_key: block {
@@ -48,7 +59,38 @@
Indicates if a given key is in @self.
"""
include?(key)
+ }
+
+ def default: default_value {
+ """
+ @default_value Default value for @self.
+
+ Sets the default value to be returned from @self for keys not in @self.
+ If @default_value is a @Block@, use its return value (called with the @Hash@ and a given key).
+ """
+
+ match default_value {
+ case Block -> @default_proc = true
+ }
+ @default = default_value
+ }
+
+ def default {
+ """
+ @return Default value for @self.
+ """
+
+ @default
+ }
+
+ def default_for: key {
+ """
+ @key Key to be used.
+ @return Default value for @key.
+ """
+
+ default(key)
}
}