Class: Sass::Script::Value::Number

Inherits:
Base
  • Object
show all
Defined in:
/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb

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 (collapse)

Attributes inherited from Base

#options, #source_range

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Base

#==, #_perform, #assert_int!, #neq, #null?, #separator, #single_eq, #to_a, #to_bool, #to_h, #unary_div, #unary_not

Constructor Details

- (Number) initialize(value, numerator_units = NO_UNITS, denominator_units = NO_UNITS)

Returns a new instance of Number

Parameters:

  • value (Numeric)

    The value of the number

  • numerator_units (::String, Array<::String>) (defaults to: NO_UNITS)
  • denominator_units (::String, Array<::String>) (defaults to: NO_UNITS)


68
69
70
71
72
73
74
75
76
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 68

def initialize(value, numerator_units = NO_UNITS, denominator_units = NO_UNITS)
  numerator_units = [numerator_units] if numerator_units.is_a?(::String)
  denominator_units = [denominator_units] if denominator_units.is_a?(::String)
  super(value)
  @numerator_units = numerator_units
  @denominator_units = denominator_units
  @options = nil
  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"]

Returns:



24
25
26
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 24

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

Returns:



19
20
21
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 19

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.

Returns:

  • (Boolean, nil)


34
35
36
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 34

def original
  @original
end

- (Numeric) value (readonly)

The Ruby value of the number.

Returns:

  • (Numeric)


14
15
16
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 14

def value
  @value
end

Class Method Details

+ epsilon

Used in checking equality of floating point numbers. Any numbers within an epsilon of each other are considered functionally equal. The value for epsilon is one tenth of the current numeric precision.



58
59
60
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 58

def self.epsilon
  @epsilon ||= 1 / (precision_factor * 10)
end

+ precision



36
37
38
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 36

def self.precision
  @precision ||= 5
end

+ precision=(digits)

Sets the number of digits of precision For example, if this is 3, 3.1415926 will be printed as 3.142.



43
44
45
46
47
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 43

def self.precision=(digits)
  @precision = digits.round
  @precision_factor = 10.0**@precision
  @epsilon = 1 / (@precision_factor * 10)
end

+ precision_factor

the precision factor used in numeric output it is derived from the precision method.



51
52
53
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 51

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.

Parameters:

Returns:

  • (Number)

    The number with the new units

Raises:



364
365
366
367
368
369
370
371
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 364

def coerce(num_units, den_units)
  Number.new(if unitless?
               value
             else
               value * coercion_factor(@numerator_units, num_units) /
                 coercion_factor(@denominator_units, den_units)
             end, num_units, den_units)
end

- (Boolean) comparable_to?(other)

Returns Whether or not this number can be compared with the other.

Parameters:

  • other (Number)

    A number to decide if it can be compared with this number.

Returns:

  • (Boolean)

    Whether or not this number can be compared with the other.



375
376
377
378
379
380
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 375

def comparable_to?(other)
  operate(other, :+)
  true
rescue Sass::UnitConversionError
  false
end

- (Value) div(other)

The SassScript / operation. Its functionality depends on the type of its argument:

Sass::Script::Value::Number
Divides this number by the other, converting units appropriately.
Sass::Script::Value
See Base#div.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:

  • (Value)

    The result of the operation



170
171
172
173
174
175
176
177
178
179
180
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 170

def div(other)
  if other.is_a? Number
    res = operate(other, :/)
    if original && other.original
      res.original = "#{original}/#{other.original}"
    end
    res
  else
    super
  end
end

- (Boolean) eq(other)

The SassScript == operation.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is equal to the other object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 200

def eq(other)
  return Bool::FALSE unless other.is_a?(Sass::Script::Value::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 Bool::FALSE
  end
  Bool.new(basically_equal?(this.value, other.value))
end

- (Boolean) eql?(other)

Hash-equality works differently than == equality for numbers. Hash-equality must be transitive, so it just compares the exact value, numerator units, and denominator units.

Returns:

  • (Boolean)


222
223
224
225
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 222

def eql?(other)
  basically_equal?(value, other.value) && numerator_units == other.numerator_units &&
    denominator_units == other.denominator_units
end

- (Boolean) gt(other)

The SassScript > operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is greater than the other

Raises:

  • (NoMethodError)

    if other is an invalid type



232
233
234
235
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 232

def gt(other)
  raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
  operate(other, :>)
end

- (Boolean) gte(other)

The SassScript >= operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is greater than or equal to the other

Raises:

  • (NoMethodError)

    if other is an invalid type



242
243
244
245
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 242

def gte(other)
  raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
  operate(other, :>=)
end

- hash



215
216
217
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 215

def hash
  [value, numerator_units, denominator_units].hash
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.

Returns:

  • (String)

    The representation



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 282

def inspect(opts = {})
  return original if original

  value = self.class.round(self.value)
  str = value.to_s

  # Ruby will occasionally print in scientific notation if the number is
  # small enough. That's technically valid CSS, but it's not well-supported
  # and confusing.
  str = ("%0.#{self.class.precision}f" % value).gsub(/0*$/, '') if str.include?('e')

  # Sometimes numeric formatting will result in a decimal number with a trailing zero (x.0)
  if str =~ /(.*)\.0$/
    str = $1
  end

  # We omit a leading zero before the decimal point in compressed mode.
  if @options && options[:style] == :compressed
    str.sub!(/^(-)?0\./, '\1.')
  end

  unitless? ? str : "#{str}#{unit_str}"
end

- (Boolean) int?

Returns Whether or not this number is an integer.

Returns:

  • (Boolean)

    Whether or not this number is an integer.



315
316
317
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 315

def int?
  basically_equal?(value % 1, 0.0)
end

- (Boolean) is_unit?(unit)

Checks whether the number has the numerator unit specified.

Examples:

number = Sass::Script::Value::Number.new(10, "px")
number.is_unit?("px") => true
number.is_unit?(nil) => false

Parameters:

  • unit (::String, nil)

    The unit the number should have or nil if the number should be unitless.

Returns:

  • (Boolean)

See Also:



334
335
336
337
338
339
340
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 334

def is_unit?(unit)
  if unit
    denominator_units.size == 0 && numerator_units.size == 1 && numerator_units.first == unit
  else
    unitless?
  end
end

Returns Whether or not this number has units that can be represented in CSS (that is, zero or one #numerator_units).

Returns:

  • (Boolean)

    Whether or not this number has units that can be represented in CSS (that is, zero or one #numerator_units).



344
345
346
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 344

def legal_units?
  (@numerator_units.empty? || @numerator_units.size == 1) && @denominator_units.empty?
end

- (Boolean) lt(other)

The SassScript < operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is less than the other

Raises:

  • (NoMethodError)

    if other is an invalid type



252
253
254
255
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 252

def lt(other)
  raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
  operate(other, :<)
end

- (Boolean) lte(other)

The SassScript <= operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is less than or equal to the other

Raises:

  • (NoMethodError)

    if other is an invalid type



262
263
264
265
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 262

def lte(other)
  raise NoMethodError.new(nil, :lte) unless other.is_a?(Number)
  operate(other, :<=)
end

- (Value) minus(other)

The SassScript binary - operation (e.g. $a - $b). Its functionality depends on the type of its argument:

Sass::Script::Value::Number
Subtracts this number from the other, converting units if possible.
Sass::Script::Value
See Base#minus.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:

  • (Value)

    The result of the operation

Raises:



115
116
117
118
119
120
121
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 115

def minus(other)
  if other.is_a? Number
    operate(other, :-)
  else
    super
  end
end

- (Number) mod(other)

The SassScript % operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Number)

    This number modulo the other

Raises:



188
189
190
191
192
193
194
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 188

def mod(other)
  if other.is_a?(Number)
    operate(other, :%)
  else
    raise NoMethodError.new(nil, :mod)
  end
end

- (Value) plus(other)

The SassScript + operation. Its functionality depends on the type of its argument:

Sass::Script::Value::Number
Adds the two numbers together, converting units if possible.
Color
Adds this number to each of the RGB color channels.
Sass::Script::Value
See Base#plus.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:

  • (Value)

    The result of the operation

Raises:



93
94
95
96
97
98
99
100
101
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 93

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)

The SassScript * operation. Its functionality depends on the type of its argument:

Sass::Script::Value::Number
Multiplies the two numbers together, converting units appropriately.
Color
Multiplies each of the RGB color channels by this number.

Parameters:

  • other (Number, Color)

    The right-hand side of the operator

Returns:

Raises:

  • (NoMethodError)

    if other is an invalid type



149
150
151
152
153
154
155
156
157
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 149

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

Returns The integer value of the number

Returns:

  • (Fixnum)

    The integer value of the number

Raises:



309
310
311
312
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 309

def to_i
  super unless int?
  value.to_i
end

- (String) to_s(opts = {})

Returns The CSS representation of this number

Returns:

  • (String)

    The CSS representation of this number

Raises:

  • (Sass::SyntaxError)

    if this number has units that can’t be used in CSS (e.g. px*in)



270
271
272
273
274
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 270

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

Returns:

  • (Number)

    The negative value of this number



133
134
135
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 133

def unary_minus
  Number.new(-value, @numerator_units, @denominator_units)
end

- (Number) unary_plus

The SassScript unary + operation (e.g. +$a).

Returns:

  • (Number)

    The value of this number



126
127
128
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 126

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

Returns:

  • (String)

    a string that represents the units in this number



386
387
388
389
390
391
392
393
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 386

def unit_str
  rv = @numerator_units.sort.join("*")
  if @denominator_units.any?
    rv << "/"
    rv << @denominator_units.sort.join("*")
  end
  rv
end

- (Boolean) unitless?

Returns Whether or not this number has no units.

Returns:

  • (Boolean)

    Whether or not this number has no units.



320
321
322
# File '/Users/ceppstei/Projects/sass-lang/.sass/lib/sass/script/value/number.rb', line 320

def unitless?
  @numerator_units.empty? && @denominator_units.empty?
end