Class: Hexable::Number
- Inherits:
-
Object
- Object
- Hexable::Number
- Defined in:
- lib/hexable.rb
Instance Method Summary collapse
- #add(x) ⇒ Object
- #base ⇒ Object
- #base32 ⇒ Object
- #binary ⇒ Object
- #decimal ⇒ Object
- #divide(x) ⇒ Object
- #hex ⇒ Object
-
#initialize(num) ⇒ Number
constructor
A new instance of Number.
- #multiply(x) ⇒ Object
- #number(num) ⇒ Object
- #octal ⇒ Object
- #subtract(x) ⇒ Object
Constructor Details
#initialize(num) ⇒ Number
Returns a new instance of Number.
5 6 7 8 |
# File 'lib/hexable.rb', line 5 def initialize(num) @number = num.eql?(nil) ? num : 0 @base = 10 end |
Instance Method Details
#add(x) ⇒ Object
43 44 45 46 47 |
# File 'lib/hexable.rb', line 43 def add(x) num = Hexable::Number.new(x).decimal num += @number return convert(num) end |
#base ⇒ Object
14 15 16 |
# File 'lib/hexable.rb', line 14 def base return @base end |
#base32 ⇒ Object
38 39 40 41 |
# File 'lib/hexable.rb', line 38 def base32 @base = 32 return @number.to_i().to_s(32) end |
#binary ⇒ Object
18 19 20 21 |
# File 'lib/hexable.rb', line 18 def binary @base = 2 return @number.to_i().to_s(2) end |
#decimal ⇒ Object
28 29 30 31 |
# File 'lib/hexable.rb', line 28 def decimal @base = 10 return Integer(@number) end |
#divide(x) ⇒ Object
61 62 63 64 65 |
# File 'lib/hexable.rb', line 61 def divide(x) num = Hexable::Number.new(x).decimal num /= @number return convert(num) end |
#hex ⇒ Object
33 34 35 36 |
# File 'lib/hexable.rb', line 33 def hex @base = 16 return @number.to_i().to_s(16) end |
#multiply(x) ⇒ Object
55 56 57 58 59 |
# File 'lib/hexable.rb', line 55 def multiply(x) num = Hexable::Number.new(x).decimal num *= @number return convert(num) end |
#number(num) ⇒ Object
10 11 12 |
# File 'lib/hexable.rb', line 10 def number(num) @number = num end |
#octal ⇒ Object
23 24 25 26 |
# File 'lib/hexable.rb', line 23 def octal @base = 8 return @number.to_i().to_s(8) end |