Sha256: 4eabe51c5539f8bac56af2ad8455a5c373f75ba5299bc7a8462745eb3e641c82

Contents?: true

Size: 755 Bytes

Versions: 1

Compression:

Stored size: 755 Bytes

Contents

class Some
  include Option

  attr_reader :value

  def initialize(value)
    @value = value
  end

  def each &block
    block.call(value)
  end

  def none?(&block)
    block.nil? ? false : super
  end

  def value_or(default=nil)
    value
  end

  def some?(type=value.class)
    value.class == type
  end

  def & other
    other.and_option(self)
  end

  def == other
    other.is_a?(Some) && value == other.value
  end

  def | other
    self
  end

  def merge(other, &block)
    other.match do |m|
      m.some { |v| block.nil? ? Some[*value, v] : Some[block.call(value, v)] }
      m.none { self }
    end
  end

  def to_s
    "Some[#{value.inspect}]"
  end

  def self.[](*values)
    new(values.size == 1 ? values.first : values)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
optional-0.0.6 lib/optional/some.rb