Class Index [+]

Quicksearch

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

PRECISION

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

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

CONVERSION_TABLE

Attributes

value[R]

The Ruby value of the number.

@return [Numeric]

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]

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]

original[RW]

The original representation of this number. For example, although the result of `1px/2px` is `0.5`, the value of `#` 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]

Public Class Methods

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

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

    # 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) 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] The numerator units to coerce this number into.

  See {\#numerator\_units}

@param den_units [Array] 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 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) 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 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) 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 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) 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 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) 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 200
200:     def gt(other)
201:       raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
202:       operate(other, :>)
203:     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 210
210:     def gte(other)
211:       raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
212:       operate(other, :>=)
213:     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 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
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 271
271:     def int?
272:       value % 1 == 0.0
273:     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 220
220:     def lt(other)
221:       raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
222:       operate(other, :<)
223:     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 230
230:     def lte(other)
231:       raise NoMethodError.new(nil, :lte) unless other.is_a?(Number)
232:       operate(other, :<=)
233:     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 91
91:     def minus(other)
92:       if other.is_a? Number
93:         operate(other, :-)
94:       else
95:         super
96:       end
97:     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 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) 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 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) 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 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() 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 265
265:     def to_i
266:       super unless int?
267:       return value
268:     end
to_s() 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 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 = {}) 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 109
109:     def unary_minus
110:       Number.new(-value, numerator_units, denominator_units)
111:     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 102
102:     def unary_plus
103:       self
104:     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 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?() click to toggle source

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

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

Private Instance Methods

coercion_factor(from_units, to_units) click to toggle source
     # File lib/sass/script/number.rb, line 357
357:     def coercion_factor(from_units, to_units)
358:       # get a list of unmatched units
359:       from_units, to_units = sans_common_units(from_units, to_units)
360: 
361:       if from_units.size != to_units.size || !convertable?(from_units | to_units)
362:         raise Sass::UnitConversionError.new("Incompatible units: '#{from_units.join('*')}' and '#{to_units.join('*')}'.")
363:       end
364: 
365:       from_units.zip(to_units).inject(1) {|m,p| m * conversion_factor(p[0], p[1]) }
366:     end
compute_units(this, other, operation) click to toggle source
     # File lib/sass/script/number.rb, line 368
368:     def compute_units(this, other, operation)
369:       case operation
370:       when :*
371:         [this.numerator_units + other.numerator_units, this.denominator_units + other.denominator_units]
372:       when :/
373:         [this.numerator_units + other.denominator_units, this.denominator_units + other.numerator_units]
374:       else  
375:         [this.numerator_units, this.denominator_units]
376:       end
377:     end
conversion_factor(from_unit, to_unit) click to toggle source
     # File lib/sass/script/number.rb, line 400
400:     def conversion_factor(from_unit, to_unit)
401:       res = CONVERSION_TABLE[CONVERTABLE_UNITS[from_unit]][CONVERTABLE_UNITS[to_unit]]
402:       return 1.0 / conversion_factor(to_unit, from_unit) if res.nil?
403:       res
404:     end
convertable?(units) click to toggle source
     # File lib/sass/script/number.rb, line 406
406:     def convertable?(units)
407:       Array(units).all?(&CONVERTABLE_UNITS.method(:include?))
408:     end
normalize!() click to toggle source
     # File lib/sass/script/number.rb, line 379
379:     def normalize!
380:       return if unitless?
381:       @numerator_units, @denominator_units = sans_common_units(numerator_units, denominator_units)
382: 
383:       @denominator_units.each_with_index do |d, i|
384:         if convertable?(d) && (u = @numerator_units.detect(&method(:convertable?)))
385:           @value /= conversion_factor(d, u)
386:           @denominator_units.delete_at(i)
387:           @numerator_units.delete_at(@numerator_units.index(u))
388:         end
389:       end
390:     end
operate(other, operation) click to toggle source
     # File lib/sass/script/number.rb, line 337
337:     def operate(other, operation)
338:       this = self
339:       if [:+, :-, :<=, :<, :>, :>=].include?(operation)
340:         if unitless?
341:           this = this.coerce(other.numerator_units, other.denominator_units)
342:         else
343:           other = other.coerce(numerator_units, denominator_units)
344:         end
345:       end
346:       # avoid integer division
347:       value = (:/ == operation) ? this.value.to_f : this.value
348:       result = value.send(operation, other.value)
349: 
350:       if result.is_a?(Numeric)
351:         Number.new(result, *compute_units(this, other, operation))
352:       else # Boolean op
353:         Bool.new(result)
354:       end
355:     end
sans_common_units(units1, units2) click to toggle source
     # File lib/sass/script/number.rb, line 410
410:     def sans_common_units(units1, units2)
411:       units2 = units2.dup
412:       # Can't just use -, because we want px*px to coerce properly to px*mm
413:       return units1.map do |u|
414:         next u unless j = units2.index(u)
415:         units2.delete_at(j)
416:         nil
417:       end.compact, units2
418:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.