### Home Classes Methods

### In Files

- lib/sass/script/number.rb

### Parent

Sass::Script::Literal

### Methods

- ::const_missing
- ::new
- ::precision
- ::precision=
- ::precision_factor
- #coerce
- #comparable_to?
- #div
- #eq
- #gt
- #gte
- #inspect
- #int?
- #legal_units?
- #lt
- #lte
- #minus
- #mod
- #plus
- #times
- #to_i
- #to_s
- #to_sass
- #unary_minus
- #unary_plus
- #unit_str
- #unitless?

### Class/Module Index

Quicksearch

- Sass
- Sass::CSS
- Sass::CacheStores
- Sass::CacheStores::Base
- Sass::CacheStores::Chain
- Sass::CacheStores::Filesystem
- Sass::CacheStores::Memory
- Sass::CacheStores::Null
- Sass::Callbacks
- Sass::Callbacks::InstanceMethods
- Sass::Engine
- Sass::Engine::Line
- Sass::Environment
- Sass::Exec
- Sass::Exec::Generic
- Sass::Exec::Sass
- Sass::Exec::SassConvert
- Sass::Exec::Scss
- Sass::Importers
- Sass::Importers::Base
- Sass::Importers::Filesystem
- Sass::Logger
- Sass::Logger::Base
- Sass::Logger::LogLevel
- Sass::Logger::LogLevel::ClassMethods
- Sass::Media
- Sass::Media::Query
- Sass::Media::QueryList
- Sass::Plugin
- Sass::Plugin::Compiler
- Sass::Plugin::Configuration
- Sass::Plugin::MerbBootLoader
- Sass::Plugin::Rack
- Sass::Plugin::StalenessChecker
- Sass::Repl
- Sass::SCSS
- Sass::SCSS::CssParser
- Sass::SCSS::Parser
- Sass::SCSS::RX
- Sass::SCSS::ScriptLexer
- Sass::SCSS::ScriptParser
- Sass::SCSS::StaticParser
- Sass::Script
- Sass::Script::ArgList
- Sass::Script::Bool
- Sass::Script::Color
- Sass::Script::CssLexer
- Sass::Script::CssParser
- Sass::Script::Funcall
- Sass::Script::Functions
- Sass::Script::Functions::EvaluationContext
- Sass::Script::Interpolation
- Sass::Script::Lexer
- Sass::Script::List
- Sass::Script::Literal
- Sass::Script::Node
- Sass::Script::Null
- Sass::Script::Number
- Sass::Script::Operation
- Sass::Script::Parser
- Sass::Script::String
- Sass::Script::StringInterpolation
- Sass::Script::UnaryOperation
- Sass::Script::Variable
- Sass::Selector
- Sass::Selector::AbstractSequence
- Sass::Selector::Attribute
- Sass::Selector::Class
- Sass::Selector::CommaSequence
- Sass::Selector::Element
- Sass::Selector::Id
- Sass::Selector::Interpolation
- Sass::Selector::Parent
- Sass::Selector::Placeholder
- Sass::Selector::Pseudo
- Sass::Selector::SelectorPseudoClass
- Sass::Selector::Sequence
- Sass::Selector::Simple
- Sass::Selector::SimpleSequence
- Sass::Selector::Universal
- Sass::Shared
- Sass::Supports
- Sass::Supports::Condition
- Sass::Supports::Declaration
- Sass::Supports::Interpolation
- Sass::Supports::Negation
- Sass::Supports::Operator
- Sass::SyntaxError
- Sass::Tree
- Sass::Tree::CharsetNode
- Sass::Tree::CommentNode
- Sass::Tree::ContentNode
- Sass::Tree::CssImportNode
- Sass::Tree::DebugNode
- Sass::Tree::DirectiveNode
- Sass::Tree::EachNode
- Sass::Tree::ExtendNode
- Sass::Tree::ForNode
- Sass::Tree::FunctionNode
- Sass::Tree::IfNode
- Sass::Tree::ImportNode
- Sass::Tree::MediaNode
- Sass::Tree::MixinDefNode
- Sass::Tree::MixinNode
- Sass::Tree::Node
- Sass::Tree::PropNode
- Sass::Tree::ReturnNode
- Sass::Tree::RootNode
- Sass::Tree::RuleNode
- Sass::Tree::SupportsNode
- Sass::Tree::TraceNode
- Sass::Tree::VariableNode
- Sass::Tree::Visitors
- Sass::Tree::Visitors::Base
- Sass::Tree::Visitors::CheckNesting
- Sass::Tree::Visitors::Convert
- Sass::Tree::Visitors::Cssize
- Sass::Tree::Visitors::DeepCopy
- Sass::Tree::Visitors::Extend
- Sass::Tree::Visitors::Perform
- Sass::Tree::Visitors::SetOptions
- Sass::Tree::Visitors::ToCss
- Sass::Tree::WarnNode
- Sass::Tree::WhileNode
- Sass::UnitConversionError
- Sass::Util
- Sass::Util::MultibyteStringScanner
- Sass::Util::StaticConditionalContext
- Sass::Util::SubsetMap
- Sass::Util::Test
- Sass::Version
- ActionController
- ActionController::Base
- Object

No matching classes.

# Sass::Script::Number

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.

### Constants

CONVERSION_TABLE
CONVERTABLE_UNITS

  A hash of unit names to their index in the conversion table

NO_UNITS

  Used so we don't allocate two new arrays for each new number.

OPERATIONS

### 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

const_missing(const) click to toggle source

Handles the deprecation warning for the PRECISION constant This can be removed in 3.2.

    # File 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

new(value, numerator_units = NO_UNITS, denominator_units = NO_UNITS) click to toggle source

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

    # File 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

precision() click to toggle source

    # File lib/sass/script/number.rb, line 38
    def self.precision
      @precision ||= 5
    end

precision=(digits) click to toggle source

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

    # File lib/sass/script/number.rb, line 45
    def self.precision=(digits)
      @precision = digits.round
      @precision_factor = 10.0**@precision
    end

precision_factor() click to toggle source

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

    # File lib/sass/script/number.rb, line 52
    def self.precision_factor
      @precision_factor ||= 10.0**precision
    end

### Public Instance Methods

coerce(num_units, den_units) click to toggle source

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

    # File 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

comparable_to?(other) click to toggle source

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

    # File lib/sass/script/number.rb, line 332
    def comparable_to?(other)
      begin
        operate(other, :+)
        true
      rescue Sass::UnitConversionError
        false
      end
    end

div(other) click to toggle source

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

    # File 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

eq(other) click to toggle source

The SassScript `==` operation.

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

    # File 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

gt(other) click to toggle source

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

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

gte(other) click to toggle source

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

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

inspect(opts = {}) click to toggle source

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

    # File lib/sass/script/number.rb, line 276
    def inspect(opts = {})
      value = self.class.round(self.value)
      unitless? ? value.to_s : "#{value}#{unit_str}"
    end

Also aliased as: to_sass
int?() click to toggle source

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

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

legal_units?() click to toggle source

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

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

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

lt(other) click to toggle source

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

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

lte(other) click to toggle source

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

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

minus(other) click to toggle source

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

    # File lib/sass/script/number.rb, line 117
    def minus(other)
      if other.is_a? Number
        operate(other, :-)
      else
        super
      end
    end

mod(other) click to toggle source

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

    # File 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

plus(other) click to toggle source

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

    # File 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

times(other) click to toggle source

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

    # File 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

to_i() click to toggle source

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

    # File lib/sass/script/number.rb, line 284
    def to_i
      super unless int?
      return value
    end

to_s(opts = {}) click to toggle source

@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`)

    # File 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

to_sass(opts = {}) click to toggle source
Alias for: inspect
unary_minus() click to toggle source

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

@return [Number] The negative value of this number

    # File lib/sass/script/number.rb, line 135
    def unary_minus
      Number.new(-value, @numerator_units, @denominator_units)
    end

unary_plus() click to toggle source

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

@return [Number] The value of this number

    # File lib/sass/script/number.rb, line 128
    def unary_plus
      self
    end

unit_str() click to toggle source

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

    # File 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

unitless?() click to toggle source

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

    # File lib/sass/script/number.rb, line 295
    def unitless?
      @numerator_units.empty? && @denominator_units.empty?
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.