Class: Dhall::Double

Inherits:
Expression show all
Defined in:
lib/dhall/ast.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Expression

#&, #*, #+, #as_dhall, #cache_key, #call, #concat, decode, #deep_merge, #deep_merge_type, #dhall_eq, #digest, #fetch, #fusion, #merge, #normalize, #resolve, #shift, #slice, #substitute, #to_proc, #|

Class Method Details

.as_dhallObject



1076
1077
1078
# File 'lib/dhall/ast.rb', line 1076

def self.as_dhall
	Builtins[:Double]
end

Instance Method Details

#===(other) ⇒ Object



1088
1089
1090
# File 'lib/dhall/ast.rb', line 1088

def ===(other)
	self == other || value === other
end

#as_jsonObject



1105
1106
1107
# File 'lib/dhall/ast.rb', line 1105

def as_json
	self
end

#coerce(other) ⇒ Object



1092
1093
1094
1095
# File 'lib/dhall/ast.rb', line 1092

def coerce(other)
	return [other, self] if other.is_a?(Double)
	[Double.new(value: other.to_f), self]
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


1097
1098
1099
# File 'lib/dhall/ast.rb', line 1097

def eql?(other)
	other.is_a?(Double) && to_cbor == other.to_cbor
end

#single?Boolean

Returns:

  • (Boolean)


1101
1102
1103
# File 'lib/dhall/ast.rb', line 1101

def single?
	[value].pack("g").unpack("g").first == value
end

#to_cbor(packer = nil) ⇒ Object



1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
# File 'lib/dhall/ast.rb', line 1113

def to_cbor(packer=nil)
	if [0, Float::INFINITY, -Float::INFINITY].include?(value) || value.nan?
		return value.to_cbor(packer)
	end

	# Dhall spec requires *not* using half-precision CBOR floats
	bytes = single? ? [0xFA, value].pack("Cg") : [0xFB, value].pack("CG")
	if packer
		packer.buffer.write(bytes)
		packer
	else
		bytes
	end
end

#to_fObject



1084
1085
1086
# File 'lib/dhall/ast.rb', line 1084

def to_f
	value
end

#to_jsonObject



1109
1110
1111
# File 'lib/dhall/ast.rb', line 1109

def to_json
	value.to_json
end

#to_sObject



1080
1081
1082
# File 'lib/dhall/ast.rb', line 1080

def to_s
	value.to_s
end