Class: Sass::Script::Number
Overview
A SassScript object representing a number. SassScript numbers can have decimal values, and can also have units. For example, 12, 1px, and 10.45em are all valid values.
Numbers can also have more complex units, such as 1px*em/in. These cannot be inputted directly in Sass code at the moment.
Constant Summary
Instance Attribute Summary
- - (Array<String>) denominator_units readonly A list of units in the denominator of the number.
- - (Array<String>) numerator_units readonly A list of units in the numerator of the number.
- - (Boolean?) original The original representation of this number.
- - (Numeric) value readonly The Ruby value of the number.
Class Method Summary
- + const_missing(const) Handles the deprecation warning for the PRECISION constant This can be removed in 3.2.
- + precision
-
+ precision(digits)
Sets the number of digits of precision For example, if this is
3,3.1415926will be printed as3.142. -
+ precision_factor
the precision factor used in numeric output it is derived from the
precisionmethod.
Instance Method Summary
- - (Number) coerce(num_units, den_units) Returns this number converted to other units.
- - (Boolean) comparable_to?(other) Whether or not this number can be compared with the other.
-
- (Literal) div(other)
The SassScript
/operation. -
- (Boolean) eq(other)
The SassScript
==operation. -
- (Boolean) gt(other)
The SassScript
>operation. -
- (Boolean) gte(other)
The SassScript
>=operation. - - (Number) initialize(value, numerator_units = NO_UNITS, denominator_units = NO_UNITS) constructor A new instance of Number.
- - (String) inspect(opts = {}) (also: #to_sass) Returns a readable representation of this number.
- - (Boolean) int? Whether or not this number is an integer.
- - (Boolean) legal_units? Whether or not this number has units that can be represented in CSS (that is, zero or one #numerator_units).
-
- (Boolean) lt(other)
The SassScript
<operation. -
- (Boolean) lte(other)
The SassScript
<=operation. -
- (Literal) minus(other)
The SassScript binary
-operation (e.g.$a - $b). -
- (Number) mod(other)
The SassScript
%operation. -
- (Literal) plus(other)
The SassScript
+operation. -
- (Number, Color) times(other)
The SassScript
*operation. - - (Fixnum) to_i The integer value of the number.
- - (String) to_s(opts = {}) The CSS representation of this number.
-
- (Number) unary_minus
The SassScript unary
-operation (e.g.-$a). -
- (Number) unary_plus
The SassScript unary
+operation (e.g.+$a). - - (String) unit_str Returns a human readable representation of the units in this number.
- - (Boolean) unitless? Whether or not this number has no units.
Methods inherited from Literal
#==, #_perform, #assert_int!, #children, #comma, #deep_copy, #neq, #options, #single_eq, #space, #to_a, #to_bool, #unary_div, #unary_not
Methods inherited from Node
#_perform, #children, #dasherize, #deep_copy, #opts, #perform
Constructor Details
- (Number) initialize(value, numerator_units = NO_UNITS, denominator_units = NO_UNITS)
A new instance of Number
73 74 75 76 77 78 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 73
def initialize(value, numerator_units = NO_UNITS, denominator_units = NO_UNITS)
super(value)
@numerator_units = numerator_units
@denominator_units = denominator_units
normalize!
end
|
Instance Attribute Details
- (Array<String>) denominator_units (readonly)
A list of units in the denominator of the number. For example, 1px*em/in*cm would return ["in", "cm"]
26 27 28 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 26
def denominator_units
@denominator_units
end
|
- (Array<String>) numerator_units (readonly)
A list of units in the numerator of the number. For example, 1px*em/in*cm would return ["px", "em"]
21 22 23 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 21
def numerator_units
@numerator_units
end
|
- (Boolean?) original
The original representation of this number. For example, although the result of 1px/2px is 0.5, the value of #original is "1px/2px".
This is only non-nil when the original value should be used as the CSS value, as in font: 1px/2px.
36 37 38 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 36
def original
@original
end
|
- (Numeric) value (readonly)
The Ruby value of the number.
16 17 18 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 16
def value
@value
end
|
Class Method Details
+ const_missing(const)
Handles the deprecation warning for the PRECISION constant This can be removed in 3.2.
58 59 60 61 62 63 64 65 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 58
def self.const_missing(const)
if const == :PRECISION
Sass::Util.sass_warn("Sass::Script::Number::PRECISION is deprecated and will be removed in a future release. Use Sass::Script::Number.precision_factor instead.")
const_set(:PRECISION, self.precision_factor)
else
super
end
end
|
+ precision
38 39 40 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 38
def self.precision
@precision ||= 3
end
|
+ precision=(digits)
Sets the number of digits of precision For example, if this is 3, 3.1415926 will be printed as 3.142.
45 46 47 48 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 45
def self.precision=(digits)
@precision = digits.round
@precision_factor = 10.0**@precision
end
|
+ precision_factor
the precision factor used in numeric output it is derived from the precision method.
52 53 54 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 52
def self.precision_factor
@precision_factor ||= 10.0**precision
end
|
Instance Method Details
- (Number) coerce(num_units, den_units)
Returns this number converted to other units. The conversion takes into account the relationship between e.g. mm and cm, as well as between e.g. in and cm.
If this number has no units, it will simply return itself with the given units.
An incompatible coercion, e.g. between px and cm, will raise an error.
321 322 323 324 325 326 327 328 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 321
def coerce(num_units, den_units)
Number.new(if unitless?
self.value
else
self.value * coercion_factor(@numerator_units, num_units) /
coercion_factor(@denominator_units, den_units)
end, num_units, den_units)
end
|
- (Boolean) comparable_to?(other)
Whether or not this number can be compared with the other.
332 333 334 335 336 337 338 339 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 332
def comparable_to?(other)
begin
operate(other, :+)
true
rescue Sass::UnitConversionError
false
end
end
|
- (Literal) div(other)
The SassScript / operation. Its functionality depends on the type of its argument:
- Number
- Divides this number by the other, converting units appropriately.
- Literal
- See Literal#div.
172 173 174 175 176 177 178 179 180 181 182 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 172
def div(other)
if other.is_a? Number
res = operate(other, :/)
if self.original && other.original
res.original = "#{self.original}/#{other.original}"
end
res
else
super
end
end
|
- (Boolean) eq(other)
The SassScript == operation.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 205
def eq(other)
return Sass::Script::Bool.new(false) unless other.is_a?(Sass::Script::Number)
this = self
begin
if unitless?
this = this.coerce(other.numerator_units, other.denominator_units)
else
other = other.coerce(@numerator_units, @denominator_units)
end
rescue Sass::UnitConversionError
return Sass::Script::Bool.new(false)
end
Sass::Script::Bool.new(this.value == other.value)
end
|
- (Boolean) gt(other)
The SassScript > operation.
226 227 228 229 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 226
def gt(other)
raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
operate(other, :>)
end
|
- (Boolean) gte(other)
The SassScript >= operation.
236 237 238 239 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 236
def gte(other)
raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
operate(other, :>=)
end
|
- (String) inspect(opts = {}) Also known as: to_sass
Returns a readable representation of this number.
This representation is valid CSS (and valid SassScript) as long as there is only one unit.
276 277 278 279 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 276
def inspect(opts = {})
value = self.class.round(self.value)
unitless? ? value.to_s : "#{value}#{unit_str}"
end
|
- (Boolean) int?
Whether or not this number is an integer.
290 291 292 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 290
def int?
value % 1 == 0.0
end
|
- (Boolean) legal_units?
Whether or not this number has units that can be represented in CSS (that is, zero or one #numerator_units).
301 302 303 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 301
def legal_units?
(@numerator_units.empty? || @numerator_units.size == 1) && @denominator_units.empty?
end
|
- (Boolean) lt(other)
The SassScript < operation.
246 247 248 249 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 246
def lt(other)
raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
operate(other, :<)
end
|
- (Boolean) lte(other)
The SassScript <= operation.
256 257 258 259 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 256
def lte(other)
raise NoMethodError.new(nil, :lte) unless other.is_a?(Number)
operate(other, :<=)
end
|
- (Literal) minus(other)
The SassScript binary - operation (e.g. $a - $b). Its functionality depends on the type of its argument:
- Number
- Subtracts this number from the other, converting units if possible.
- Literal
- See Literal#minus.
117 118 119 120 121 122 123 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 117
def minus(other)
if other.is_a? Number
operate(other, :-)
else
super
end
end
|
- (Number) mod(other)
The SassScript % operation.
190 191 192 193 194 195 196 197 198 199 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 190
def mod(other)
if other.is_a?(Number)
unless other.unitless?
raise Sass::UnitConversionError.new("Cannot modulo by a number with units: #{other.inspect}.")
end
operate(other, :%)
else
raise NoMethodError.new(nil, :mod)
end
end
|
- (Literal) plus(other)
The SassScript + operation. Its functionality depends on the type of its argument:
- Number
- Adds the two numbers together, converting units if possible.
- Color
- Adds this number to each of the RGB color channels.
- Literal
- See Literal#plus.
95 96 97 98 99 100 101 102 103 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 95
def plus(other)
if other.is_a? Number
operate(other, :+)
elsif other.is_a?(Color)
other.plus(self)
else
super
end
end
|
- (Number, Color) times(other)
151 152 153 154 155 156 157 158 159 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 151
def times(other)
if other.is_a? Number
operate(other, :*)
elsif other.is_a? Color
other.times(self)
else
raise NoMethodError.new(nil, :times)
end
end
|
- (Fixnum) to_i
The integer value of the number
284 285 286 287 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 284
def to_i
super unless int?
return value
end
|
- (String) to_s(opts = {})
The CSS representation of this number
264 265 266 267 268 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 264
def to_s(opts = {})
return original if original
raise Sass::SyntaxError.new("#{inspect} isn't a valid CSS value.") unless legal_units?
inspect
end
|
- (Number) unary_minus
The SassScript unary - operation (e.g. -$a).
135 136 137 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 135
def unary_minus
Number.new(-value, @numerator_units, @denominator_units)
end
|
- (Number) unary_plus
The SassScript unary + operation (e.g. +$a).
128 129 130 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 128
def unary_plus
self
end
|
- (String) unit_str
Returns a human readable representation of the units in this number. For complex units this takes the form of: numerator_unit1 * numerator_unit2 / denominator_unit1 * denominator_unit2
345 346 347 348 349 350 351 352 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 345
def unit_str
rv = @numerator_units.sort.join("*")
if @denominator_units.any?
rv << "/"
rv << @denominator_units.sort.join("*")
end
rv
end
|
- (Boolean) unitless?
Whether or not this number has no units.
295 296 297 |
# File '/var/www/sass-pages/.sass/lib/sass/script/number.rb', line 295
def unitless?
@numerator_units.empty? && @denominator_units.empty?
end
|