Module: Sass::Script::Functions

Defined in:
lib/sassy-fractions.rb

Overview

Sassy math Functions

Instance Method Summary collapse

Instance Method Details

#denominator(number) ⇒ Object



18
19
20
21
# File 'lib/sassy-fractions.rb', line 18

def denominator(number)
  num, den = number.value.fraction
  Sass::Script::Number.new(den)
end

#gcd(a, b) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/sassy-fractions.rb', line 31

def gcd(a, b)
  a = a.value
  b = b.value
  a,b = b,a if a < b
  a,b = b,a%b while a%b != 0
  Sass::Script::Number.new(b)
end

#lcm(a, b) ⇒ Object



38
39
40
41
# File 'lib/sassy-fractions.rb', line 38

def lcm(a, b)
  result = (a.value * b.value) / gcd(a, b)
  Sass::Script::Number.new(result)
end

#numerator(number) ⇒ Object

Fractions



15
16
17
# File 'lib/sassy-fractions.rb', line 15

def numerator(number)
  Sass::Script::Number.new(number.value.fraction.first)
end

#to_decimal(fraction) ⇒ Object



26
27
28
29
# File 'lib/sassy-fractions.rb', line 26

def to_decimal(fraction)
  fraction = fraction.value.to_f
  Sass::Script::Number.new(fraction)
end

#to_fraction(number) ⇒ Object



22
23
24
25
# File 'lib/sassy-fractions.rb', line 22

def to_fraction(number)
  result = numerator(number).to_s + '/' + denominator(number).to_s
  Sass::Script::String.new(result)
end