Class | Sass::Script::Number
In: | lib/sass/script/number.rb
Parent: | Literal

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.

### Methods

coerce comparable_to? div eq gt gte inspect int? legal_units? lt lte minus mod new plus times to_i to_s to_sass unary_minus unary_plus unit_str unitless?

### Constants

PRECISION | = | 1000.0 |  | The precision with which numbers will be printed to CSS files. For example, if this is `1000.0`, `3.1415926` will be printed as `3.142`. @api public
CONVERTABLE_UNITS | = | {"in" => 0, "cm" => 1, "pc" => 2, "mm" => 3, "pt" => 4} |  | A hash of unit names to their index in the conversion table
CONVERSION_TABLE | = | [[ 1, 2.54, 6, 25.4, 72 ], # in [ nil, 1, 2.36220473, 10, 28.3464567], # cm [ nil, nil, 1, 4.23333333, 12 ], # pc [ nil, nil, nil, 1, 2.83464567], # mm [ nil, nil, nil, nil, 1 ]]

### Attributes

denominator_units | [R] | A list of units in the denominator of the number. For example, `1px*em/in*cm` would return `["in", "cm"]` @return [Array<String>]
numerator_units | [R] | A list of units in the numerator of the number. For example, `1px*em/in*cm` would return `["px", "em"]` @return [Array<String>]
original | [RW] | 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`. @return [Boolean, nil]
value | [R] | The Ruby value of the number. @return [Numeric]

### Public Class methods

new(value, numerator_units = [], denominator_units = [])

@param value [Numeric] The value of the number @param numerator_units [Array<String>] See \{numerator_units} @param denominator_units [Array<String>] See \{denominator_units}

[Source]

        # File lib/sass/script/number.rb, line 47
    47:     def initialize(value, numerator_units = [], denominator_units = [])
    48:       super(value)
    49:       @numerator_units = numerator_units
    50:       @denominator_units = denominator_units
    51:       normalize!
    52:     end

### Public Instance methods

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.

@param num_units [Array<String>] The numerator units to coerce this number into.

      See {\#numerator\_units}

@param den_units [Array<String>] The denominator units to coerce this number into.

      See {\#denominator\_units}

@return [Number] The number with the new units @raise [Sass::UnitConversionError] if the given units are incompatible with the number‘s

      current units

[Source]

         # File lib/sass/script/number.rb, line 302
    302:     def coerce(num_units, den_units)
    303:       Number.new(if unitless?
    304:                    self.value
    305:                  else
    306:                    self.value * coercion_factor(self.numerator_units, num_units) /
    307:                      coercion_factor(self.denominator_units, den_units)
    308:                  end, num_units, den_units)
    309:     end

comparable_to?(other)

@param other [Number] A number to decide if it can be compared with this number. @return [Boolean] Whether or not this number can be compared with the other.

[Source]

         # File lib/sass/script/number.rb, line 313
    313:     def comparable_to?(other)
    314:       begin
    315:         operate(other, :+)
    316:         true
    317:       rescue Sass::UnitConversionError
    318:         false
    319:       end
    320:     end

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

@param other [Literal] The right-hand side of the operator @return [Literal] The result of the operation

[Source]

         # File lib/sass/script/number.rb, line 146
    146:     def div(other)
    147:       if other.is_a? Number
    148:         res = operate(other, :/)
    149:         if self.original && other.original && context != :equals
    150:           res.original = "#{self.original}/#{other.original}"
    151:         end
    152:         res
    153:       else
    154:         super
    155:       end
    156:     end

eq(other)

The SassScript `==` operation.

@param other [Literal] The right-hand side of the operator @return [Boolean] Whether this number is equal to the other object

[Source]

         # File lib/sass/script/number.rb, line 179
    179:     def eq(other)
    180:       return Sass::Script::Bool.new(false) unless other.is_a?(Sass::Script::Number)
    181:       this = self
    182:       begin
    183:         if unitless?
    184:           this = this.coerce(other.numerator_units, other.denominator_units)
    185:         else
    186:           other = other.coerce(numerator_units, denominator_units)
    187:         end
    188:       rescue Sass::UnitConversionError
    189:         return Sass::Script::Bool.new(false)
    190:       end
    191:
    192:       Sass::Script::Bool.new(this.value == other.value)
    193:     end

gt(other)

The SassScript `>` operation.

@param other [Number] The right-hand side of the operator @return [Boolean] Whether this number is greater than the other @raise [NoMethodError] if `other` is an invalid type

[Source]

         # File lib/sass/script/number.rb, line 200
    200:     def gt(other)
    201:       raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
    202:       operate(other, :>)
    203:     end

gte(other)

The SassScript `>=` operation.

@param other [Number] The right-hand side of the operator @return [Boolean] Whether this number is greater than or equal to the other @raise [NoMethodError] if `other` is an invalid type

[Source]

         # File lib/sass/script/number.rb, line 210
    210:     def gte(other)
    211:       raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
    212:       operate(other, :>=)
    213:     end

inspect(opts = {})

Returns a readable representation of this number.

This representation is valid CSS (and valid SassScript) as long as there is only one unit.

@return [String] The representation

[Source]

         # File lib/sass/script/number.rb, line 250
    250:     def inspect(opts = {})
    251:       value =
    252:         if self.value.is_a?(Float) && (self.value.infinite? || self.value.nan?)
    253:           self.value
    254:         elsif int?
    255:           self.value.to_i
    256:         else
    257:           (self.value * PRECISION).round / PRECISION
    258:         end
    259:       "#{value}#{unit_str}"
    260:     end

int?()

@return [Boolean] Whether or not this number is an integer.

[Source]

         # File lib/sass/script/number.rb, line 271
    271:     def int?
    272:       value % 1 == 0.0
    273:     end

legal_units?()

@return [Boolean] Whether or not this number has units that can be represented in CSS

      (that is, zero or one \{#numerator\_units}).

[Source]

         # File lib/sass/script/number.rb, line 282
    282:     def legal_units?
    283:       (numerator_units.empty? || numerator_units.size == 1) && denominator_units.empty?
    284:     end

lt(other)

The SassScript `<` operation.

@param other [Number] The right-hand side of the operator @return [Boolean] Whether this number is less than the other @raise [NoMethodError] if `other` is an invalid type

[Source]

         # File lib/sass/script/number.rb, line 220
    220:     def lt(other)
    221:       raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
    222:       operate(other, :<)
    223:     end

lte(other)

The SassScript `<=` operation.

@param other [Number] The right-hand side of the operator @return [Boolean] Whether this number is less than or equal to the other @raise [NoMethodError] if `other` is an invalid type

[Source]

         # File lib/sass/script/number.rb, line 230
    230:     def lte(other)
    231:       raise NoMethodError.new(nil, :lte) unless other.is_a?(Number)
    232:       operate(other, :<=)
    233:     end

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

@param other [Literal] The right-hand side of the operator @return [Literal] The result of the operation @raise [Sass::UnitConversionError] if `other` is a number with incompatible units

[Source]

        # File lib/sass/script/number.rb, line 91
    91:     def minus(other)
    92:       if other.is_a? Number
    93:         operate(other, :-)
    94:       else
    95:         super
    96:       end
    97:     end

mod(other)

The SassScript `%` operation.

@param other [Number] The right-hand side of the operator @return [Number] This number modulo the other @raise [NoMethodError] if `other` is an invalid type @raise [Sass::UnitConversionError] if `other` has any units

[Source]

         # File lib/sass/script/number.rb, line 164
    164:     def mod(other)
    165:       if other.is_a?(Number)
    166:         unless other.unitless?
    167:           raise Sass::UnitConversionError.new("Cannot modulo by a number with units: #{other.inspect}.")
    168:         end
    169:         operate(other, :%)
    170:       else
    171:         raise NoMethodError.new(nil, :mod)
    172:       end
    173:     end

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

@param other [Literal] The right-hand side of the operator @return [Literal] The result of the operation @raise [Sass::UnitConversionError] if `other` is a number with incompatible units

[Source]

        # File lib/sass/script/number.rb, line 69
    69:     def plus(other)
    70:       if other.is_a? Number
    71:         operate(other, :+)
    72:       elsif other.is_a?(Color)
    73:         other.plus(self)
    74:       else
    75:         super
    76:       end
    77:     end

times(other)

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

{Number} : Multiplies the two numbers together, converting units appropriately.

{Color} : Multiplies each of the RGB color channels by this number.

@param other [Number, Color] The right-hand side of the operator @return [Number, Color] The result of the operation @raise [NoMethodError] if `other` is an invalid type

[Source]

         # File lib/sass/script/number.rb, line 125
    125:     def times(other)
    126:       if other.is_a? Number
    127:         operate(other, :*)
    128:       elsif other.is_a? Color
    129:         other.times(self)
    130:       else
    131:         raise NoMethodError.new(nil, :times)
    132:       end
    133:     end

to_i()

@return [Fixnum] The integer value of the number @raise [Sass::SyntaxError] if the number isn‘t an integer

[Source]

         # File lib/sass/script/number.rb, line 265
    265:     def to_i
    266:       super unless int?
    267:       return value
    268:     end

to_s()

@return [String] The CSS representation of this number @raise [Sass::SyntaxError] if this number has units that can‘t be used in CSS

      (e.g. `px*in`)

[Source]

         # File lib/sass/script/number.rb, line 238
    238:     def to_s
    239:       return original if original
    240:       raise Sass::SyntaxError.new("#{inspect} isn't a valid CSS value.") unless legal_units?
    241:       inspect
    242:     end

to_sass(opts = {})

Alias for inspect

unary_minus()

The SassScript unary `-` operation (e.g. `-$a`).

@return [Number] The negative value of this number

[Source]

         # File lib/sass/script/number.rb, line 109
    109:     def unary_minus
    110:       Number.new(-value, numerator_units, denominator_units)
    111:     end

unary_plus()

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

@return [Number] The value of this number

[Source]

         # File lib/sass/script/number.rb, line 102
    102:     def unary_plus
    103:       self
    104:     end

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 @return [String] a string that represents the units in this number

[Source]

         # File lib/sass/script/number.rb, line 326
    326:     def unit_str
    327:       rv = numerator_units.sort.join("*")
    328:       if denominator_units.any?
    329:         rv << "/"
    330:         rv << denominator_units.sort.join("*")
    331:       end
    332:       rv
    333:     end

unitless?()

@return [Boolean] Whether or not this number has no units.

[Source]

         # File lib/sass/script/number.rb, line 276
    276:     def unitless?
    277:       numerator_units.empty? && denominator_units.empty?
    278:     end

[Validate]