Class | Sass::Script::Value::Number |
In: |
lib/sass/script/value/number.rb
|
Parent: | Base |
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.
NO_UNITS | = | [] | Used so we don‘t allocate two new arrays for each new number. | |
OPERATIONS | = | [:+, :-, :<=, :<, :>, :>=, :%] | ||
MUTUALLY_CONVERTIBLE | = | {} | A hash from each known unit to the set of units that it‘s mutually convertible with. | |
CONVERSION_TABLE | = | {} | A two-dimensional hash from two units to the conversion ratio between them. Multiply `X` by `CONVERSION_TABLE[X][Y]` to convert it to `Y`. |
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] |
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
@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.
The SassScript `/` operation. Its functionality depends on the type of its argument:
{Number} : Divides this number by the other, converting units appropriately.
{Value} : See {Value::Base#div}.
@param other [Value] The right-hand side of the operator @return [Value] The result of the operation
The SassScript `==` operation.
@param other [Value] The right-hand side of the operator @return [Boolean] Whether this number is equal to the other object
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.
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
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
Checks whether the number has the numerator unit specified.
@example
number = Sass::Script::Value::Number.new(10, "px") number.is_unit?("px") => true number.is_unit?(nil) => false
@param unit [::String, nil] The unit the number should have or nil if the number
should be unitless.
@see Number#unitless? The unitless? method may be more readable.
@return [Boolean] Whether or not this number has units that can be represented in CSS
(that is, zero or one \{#numerator\_units}).
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
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
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.
{Value} : See {Value::Base#minus}.
@param other [Value] The right-hand side of the operator @return [Value] The result of the operation @raise [Sass::UnitConversionError] if `other` is a number with incompatible units
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 incompatible units
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.
{Value} : See {Value::Base#plus}.
@param other [Value] The right-hand side of the operator @return [Value] The result of the operation @raise [Sass::UnitConversionError] if `other` is a number with incompatible units
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
@return [Integer] The integer value of the number @raise [Sass::SyntaxError] if the number isn‘t an integer
@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`)
The SassScript unary `-` operation (e.g. `-$a`).
@return [Number] The negative value of this number
The SassScript unary `+` operation (e.g. `+$a`).
@return [Number] The value of this number
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