Class: Hexable::Number

Inherits:
Object
  • Object
show all
Defined in:
lib/hexable.rb

Instance Method Summary collapse

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

#baseObject



14
15
16
# File 'lib/hexable.rb', line 14

def base
  return @base
end

#base32Object



38
39
40
41
# File 'lib/hexable.rb', line 38

def base32
  @base = 32
  return @number.to_i().to_s(32)
end

#binaryObject



18
19
20
21
# File 'lib/hexable.rb', line 18

def binary
  @base = 2
  return @number.to_i().to_s(2)
end

#decimalObject



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

#hexObject



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

#octalObject



23
24
25
26
# File 'lib/hexable.rb', line 23

def octal
  @base = 8
  return @number.to_i().to_s(8)
end

#subtract(x) ⇒ Object



49
50
51
52
53
# File 'lib/hexable.rb', line 49

def subtract(x)
  num = Hexable::Number.new(x).decimal
  num -= @number
  return convert(num)
end