lib/hash.fy in fancy-0.6.0 vs lib/hash.fy in fancy-0.7.0

- old
+ new

@@ -4,10 +4,13 @@ Maps a key to a value. """ include: Fancy Enumerable + alias_method: 'get_slot: for: 'at: + alias_method: 'set_slot:value: for: 'at:put: + def [key] { """ @key Key for value to get. @return Value for given @key or @nil, if @key not set. @@ -23,17 +26,48 @@ @else_block @Block@ to be called if @key is not found. @return Value for @key or value of calling @else_block, if @key is not found. Returns the value for a given key. If the key is not found, calls @else_block and returns the value it yields. + + Example: + <['foo => 'bar]> at: 'foo else: { 42 } # => 'bar + <['foo => 'bar]> at: 'unknown else: { 42 } # => 42 + <['nil => nil]> at: 'nil else: { 'not_found } # => nil """ if: (includes?: key) then: { at: key - } else: else_block + } else: { + else_block call: [key] + } } + alias_method: 'fetch:else: for: 'at:else: + + def at: key else_put: else_block { + """ + @key Key of value to get. + @else_block @Block@ that gets called and its value inserted into @self if @key not in @self. + + Example: + h = <['foo => 'bar]> + h at: 'foo else_put: { 42 } # => 'bar + h['foo] # => 'bar + h at: 'undefined else_put: { 42 } # => 42 + h['undefined] # => 42 + """ + + if: (includes?: key) then: { + at: key + } else: { + at: key put: $ else_block call: [key] + } + } + + alias_method: 'fetch:else_put: for: 'at:else_put: + def each: block { """ @block @Block@ to be called with each key and value in @self. @return @self Calls a given @Block@ with each key and value. @@ -83,20 +117,10 @@ """ map: |pair| { pair } } - def to_s { - """ - @return @String@ representation of @self. - - Returns a @String@ representation of a @Hash@. - """ - - to_a to_s - } - def to_object { """ @return New @Object@ with slots defined by keys and values in @self. Creates and returns a new @Object@ with slot names and values based on keys and values in @self. @@ -136,25 +160,7 @@ Example: <['foo => 1, 'bar => 2, 'baz => 42]> values_at: ('foo, 'baz) # => [1, 42] """ keys map: |k| { at: k } - } - - def fetch: key else: else_block { - """ - @key Key of value to get. - @else_block @Block@ that gets called if @key not in @self. - - Example: - <['foo => 'bar]> fetch: 'foo else: { 42 } # => 'bar - <['foo => 'bar]> fetch: 'unknown else: { 42 } # => 42 - <['nil => nil]> fetch: 'nil else: { 'not_found } # => nil - """ - - if: (includes?: key) then: { - at: key - } else: { - else_block call: [key] - } } }