Class: Fixnum
- Inherits:
-
Object
- Object
- Fixnum
- Defined in:
- lib/hexa/core_ext/fixnum.rb
Instance Method Summary collapse
-
#factorial ⇒ Fixnum
(also: #!)
Allows tou to know the factorial of self.
-
#inverse ⇒ Float
(also: #reverse)
Inverse of self.
-
#opposite ⇒ Fixnum
The opposite of self.
-
#prime? ⇒ Boolean
Predicate method which allows you to know if the number is a prime number.
-
#sqrt ⇒ Float
The square root of self.
Instance Method Details
#factorial ⇒ Fixnum Also known as: !
Allows tou to know the factorial of self
5 6 7 8 9 10 11 |
# File 'lib/hexa/core_ext/fixnum.rb', line 5 def factorial if self == 1 or self == 0 return 1 else return self * (self - 1).factorial end end |
#inverse ⇒ Float Also known as: reverse
Returns inverse of self.
45 46 47 48 49 50 51 |
# File 'lib/hexa/core_ext/fixnum.rb', line 45 def inverse if self == 0 return 0 else return 1 / self end end |
#opposite ⇒ Fixnum
Returns the opposite of self.
40 41 42 |
# File 'lib/hexa/core_ext/fixnum.rb', line 40 def opposite - self end |
#prime? ⇒ Boolean
Predicate method which allows you to know if the number is a prime number
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hexa/core_ext/fixnum.rb', line 22 def prime? if self < 2 false elsif self == 2 true elsif self % 2 == 0 false else i = 3 while i*i <= self return false if self % i == 0 i += 2 end return true end end |
#sqrt ⇒ Float
Returns the square root of self.
16 17 18 |
# File 'lib/hexa/core_ext/fixnum.rb', line 16 def sqrt Math.sqrt(self) end |