Sha256: 92202c75ad4b1b70427f00cac48172fab6b2347d800e3b1baee433846b26f196

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

class Hash {
  """
  Class for Hashes (HashMaps / Dictionaries).
  Maps a key to a value.
  """

  include: FancyEnumerable

  def [key] {
    """
    @key Key for value to get.
    @return Value for given @key or @nil, if @key not set.

    Returns the value for a given key.
    """

    at: key
  }

  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.
    """

    if: (block arity == 1) then: {
      keys each: |key| {
        block call: [[key, at: key]]
      }
    } else: {
      keys each: |key| {
        block call: [key, at: key]
      }
    }
    self
  }

  def each_key: block {
    """
    @block @Block@ to be called with each key in @self.
    @return @self

    Calls a given @Block@ with each key.
    """

    keys each: block
    self
  }

  def each_value: block {
    """
    @block @Block@ to be called with each value in @self.
    @return @self

    Calls a given @Block@ with each value.
    """

    values each: block
    self
  }

  def to_a {
    """
    @return @Array@ of all key-value pairs in @self.

    Returns an @Array@ of the key-value pairs in a @Hash@.
    """

    map: |pair| { pair }
  }

  def to_s {
    """
    @return @String@ representation of @self.

    Returns a @String@ representation of a @Hash@.
    """

    to_a to_s
  }

  def inspect {
    str = "<["
    each: |key val| {
      str << (key inspect)
      str << " => "
      str << (val inspect)
    } in_between: {
      str << ", "
    }
    str << "]>"
    str
  }

  def values_at: keys {
    """
    @keys Collection of keys to get the values for.
    @return @Array@ of all values for the given keys.

    Example usage:
      <['foo => 1, 'bar => 2, 'baz => 42]> values_at: ('foo, 'baz) # => [1, 42]
    """

    keys map: |k| { at: k }
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fancy-0.4.0 lib/hash.fy