class BSON::Decimal128

def <=>(other)

def <=>(other)
  to_d <=> case other
    when Decimal128
      other.to_d
    else
      other
    end
end

def ==(other)

Other tags:
    Since: - 4.2.0

Returns:
  • (true, false) - If the objects are equal.

Parameters:
  • other (Object) -- The object to check against.

Other tags:
    Example: Check if the decimal128 object is equal to the other. -
def ==(other)
  return false unless other.is_a?(Decimal128)
  @high == other.instance_variable_get(:@high) &&
      @low == other.instance_variable_get(:@low)
end

def as_extended_json(**_options)

Returns:
  • (Hash) - The extended json representation.

Options Hash: (**opts)
  • :mode (nil | :relaxed | :legacy) -- Serialization mode
def as_extended_json(**_options)
  { EXTENDED_JSON_KEY => to_s }
end

def as_json(*args)

Returns:
  • (String | nil) - The decimal128 as a String or nil for non-representable numbers.

Other tags:
    Example: Get the Decimal128 as a JSON-serializable object. -
def as_json(*args)
  value = to_s
  value unless %w[NaN Infinity -Infinity].include?(value)
end

def bson_type

Get the BSON type for Decimal128.
def bson_type
  BSON_TYPE
end

def from_bits(low, high)

Other tags:
    Since: - 4.2.0

Returns:
  • (BSON::Decimal128) - The new decimal128.

Parameters:
  • low (Integer) -- The low order bits.
  • high (Integer) -- The high order bits.

Other tags:
    Example: Create a Decimal128 from high and low bits. -
def from_bits(low, high)
  decimal = allocate
  decimal.send(:set_bits, low, high)
  decimal
end

def from_bson(buffer, **options)

Other tags:
    Since: - 4.2.0

Returns:
  • (BSON::Decimal128) - The decimal object.

Options Hash: (**options)
  • :mode (nil | :bson) -- Decoding mode to use.

Parameters:
  • buffer (ByteBuffer) -- The byte buffer.

Other tags:
    Example: Get the decimal128 from BSON. -
def from_bson(buffer, **options)
  from_bits(*buffer.get_decimal128_bytes.unpack('Q<*'))
end

def from_string(string)

Other tags:
    Since: - 4.2.0

Returns:
  • (BSON::Decimal128) - The new decimal128.

Raises:
  • (BSON::Error:InvalidDecimal128String) - If the provided string is invalid.

Parameters:
  • string (String) -- The string to parse.

Other tags:
    Example: Create a Decimal128 from a string. -
def from_string(string)
  from_bits(*Builder::FromString.new(string).bits)
end

def hash

Other tags:
    Since: - 4.2.0

Returns:
  • (Integer) - The hash value.

Other tags:
    Example: Get the hash value. -
def hash
  num = @high << 64
  num |= @low
  num.hash
end

def initialize(object)

Other tags:
    Since: - 4.2.0

Raises:
  • (BSON::Error::InvalidDecimal128Argument) - When argument is not a String or BigDecimal.

Parameters:
  • object (String, BigDecimal) -- The BigDecimal or String to use for

Other tags:
    Example: Create a Decimal128 from a BigDecimal. -
def initialize(object)
  if object.is_a?(String)
    set_bits(*Builder::FromString.new(object).bits)
  elsif object.is_a?(BigDecimal)
    set_bits(*Builder::FromBigDecimal.new(object).bits)
  else
    raise Error::InvalidDecimal128Argument.new
  end
end

def inspect

Other tags:
    Since: - 4.2.0

Returns:
  • (String) - The decimal as a string.

Other tags:
    Example: Inspect the decimal128 object. -
def inspect
  "BSON::Decimal128('#{to_s}')"
end

def set_bits(low, high)

def set_bits(low, high)
  @low = low
  @high = high
end

def to_bson(buffer = ByteBuffer.new)

Other tags:
    Since: - 4.2.0

Other tags:
    See: http://bsonspec.org/#/specification -

Returns:
  • (BSON::ByteBuffer) - The buffer with the encoded object.

Other tags:
    Example: Get the raw bson bytes in a buffer. -
def to_bson(buffer = ByteBuffer.new)
  buffer.put_decimal128(@low, @high)
end

def to_d

Returns:
  • (BigDecimal) - The decimal as a BigDecimal.
def to_d
  @big_decimal ||= BigDecimal(to_s)
end

def to_s

Other tags:
    Since: - 4.2.0

Returns:
  • (String) - The decimal128 as a string.

Other tags:
    Example: Get the decimal128 as a string. -
def to_s
  @string ||= Builder::ToString.new(self).string
end