Class: Numeric

Inherits:
Object show all
Defined in:
lib/open_classes/numeric/dozen.rb,
lib/open_classes/numeric/is_ascii.rb,
lib/open_classes/numeric/dice_back.rb,
lib/open_classes/numeric/to_hex_table.rb,
lib/open_classes/numeric/to_oct_table.rb,
lib/open_classes/numeric/to_digit_table.rb,
lib/open_classes/numeric/to_binary_table.rb,
lib/open_classes/numeric/to_hex_html_table.rb,
lib/open_classes/numeric/to_oct_html_table.rb,
lib/open_classes/numeric/to_digit_html_table.rb,
lib/open_classes/numeric/to_binary_html_table.rb

Overview

Numeric

Constant Summary collapse

DICE =

dice number mappings

{ 1 => 6, 2 => 5, 3 => 4, 4 => 3, 5 => 2, 6 => 1 }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_binary_html_table(from = 1, to = 10) ⇒ Object

binary table

Examples

1 to 3 case

Numeric.to_binary_html_table(1, 3)

result

<table>
  <tr>
    <th>10digit</th>
    <th>2digit</th>
  </tr>
  <tr>
    <td>255</td>
    <td>0000000011111111</td>
  </tr>
  <tr>
    <td>256</td>
    <td>0000000100000000</td>
  </tr>
</table>


30
31
32
33
34
35
36
37
38
# File 'lib/open_classes/numeric/to_binary_html_table.rb', line 30

def self.to_binary_html_table(from = 1, to = 10)
  ret = []
  size = to.to_s(2).size - 1
  pad = (size / 8 + 1) * 8
  ret << "<table>\n  <tr>\n    <th>10digit</th>\n    <th>2digit</th>\n  </tr>"

  (from..to).each { |i|ret << "  <tr>\n    <td>#{i}</td>\n    <td>#{i.to_s(2).rjust(pad, '0')}</td>\n  </tr>" }
  ret.join("\n") + "\n</table>\n"
end

.to_binary_table(from = 1, to = 10) ⇒ Object

return is binary table

Examples

1 to 3 case

Numeric.to_binary_table(1, 3)

result

|10digit|2digit  |
|1      |00000001|
|2      |00000010|
|3      |00000011|


20
21
22
23
24
25
26
27
28
# File 'lib/open_classes/numeric/to_binary_table.rb', line 20

def self.to_binary_table(from = 1, to = 10)
  ret = []
  size = to.to_s(2).size - 1
  pad = (size / 8 + 1) * 8
  ret << '|10digit|2digit|'
  (from..to).each { |i|ret << "|#{i}|#{i.to_s(2).rjust(pad, '0')}|" }
  joined = ret.join("\n") + "\n"
  joined.justify_table(:right)
end

.to_digit_html_table(from = 1, to = 10) ⇒ Object

return is digit html table

Examples

255 to 256 case

Numeric.to_digit_html_table(255, 256)

result

<table>
  <tr>
    <th>10digit</th>
    <th>2digit</th>
    <th>8digit</th>
    <th>16digit</th>
  </tr>
  <tr>
    <td>255</td>
    <td>0000000011111111</td>
    <td>377</td>
    <td>00ff</td>
  </tr>
  <tr>
    <td>256</td>
    <td>0000000100000000</td>
    <td>400</td>
    <td>0100</td>
  </tr>
</table>


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/open_classes/numeric/to_digit_html_table.rb', line 36

def self.to_digit_html_table(from = 1, to = 10)
  ret = []
  binary_size = to.to_s(2).size - 1
  binary_pad = (binary_size / 8 + 1) * 8
  oct_size = to.to_s(8).size
  hex_size = to.to_s(16).size - 1
  hex_pad = (hex_size / 4 + 1) * 4
  ret << "<table>\n  <tr>\n    <th>10digit</th>\n    <th>2digit</th>\n    <th>8digit</th>\n    <th>16digit</th>\n  </tr>"
  (from..to).each { |i|ret << "  <tr>\n    <td>#{i}</td>\n    <td>#{i.to_s(2).rjust(binary_pad, '0')}</td>\n    <td>#{i.to_s(8).rjust(oct_size, '0')}</td>\n    <td>#{i.to_s(16).rjust(hex_pad, '0')}</td>\n  </tr>" }
  ret.join("\n") + "\n</table>\n"
end

.to_digit_table(from = 1, to = 10) ⇒ Object

return is digit table

Examples

255 to 256 case

Numeric.to_digit_table(255, 256)

result

|10digit|          2digit|8digit|16digit|
|    255|0000000011111111|   377|   00ff|
|    256|0000000100000000|   400|   0100|


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/open_classes/numeric/to_digit_table.rb', line 19

def self.to_digit_table(from = 1, to = 10)
  ret = []
  binary_size = to.to_s(2).size - 1
  binary_pad = (binary_size / 8 + 1) * 8
  oct_size = to.to_s(8).size
  hex_size = to.to_s(16).size - 1
  hex_pad = (hex_size / 4 + 1) * 4
  ret << '|10digit|2digit|8digit|16digit|'
  (from..to).each { |i|ret << "|#{i}|#{i.to_s(2).rjust(binary_pad, '0')}|#{i.to_s(8).rjust(oct_size, '0')}|#{i.to_s(16).rjust(hex_pad, '0')}|" }
  joined = ret.join("\n") + "\n"
  joined.justify_table(:right)
end

.to_hex_html_table(from = 1, to = 10) ⇒ Object

return is hex table

Examples

65535 to 65536 case

Numeric.to_hex_html_table(65535, 65536)

result

<table>
  <tr>
    <th>10digit</th>
    <th>16digit</th>
  </tr>
  <tr>
    <td>65535</td>
    <td>0000ffff</td>
  </tr>
  <tr>
    <td>65536</td>
    <td>00010000</td>
  </tr>
</table>


30
31
32
33
34
35
36
37
# File 'lib/open_classes/numeric/to_hex_html_table.rb', line 30

def self.to_hex_html_table(from = 1, to = 10)
  ret = []
  size = to.to_s(16).size - 1
  pad = (size / 4 + 1) * 4
  ret << "<table>\n  <tr>\n    <th>10digit</th>\n    <th>16digit</th>\n  </tr>"
  (from..to).each { |i|ret << "  <tr>\n    <td>#{i}</td>\n    <td>#{i.to_s(16).rjust(pad, '0')}</td>\n  </tr>" }
  ret.join("\n") + "\n</table>\n"
end

.to_hex_table(from = 1, to = 10) ⇒ Object

return is hex table

Examples

65535 to 65536 case

Numeric.to_hex_table(65535, 65536)

result

|10digit| 16digit|
|  65535|0000ffff|
|  65536|00010000|


19
20
21
22
23
24
25
26
27
# File 'lib/open_classes/numeric/to_hex_table.rb', line 19

def self.to_hex_table(from = 1, to = 10)
  ret = []
  size = to.to_s(16).size - 1
  pad = (size / 4 + 1) * 4
  ret << '|10digit|16digit|'
  (from..to).each { |i|ret << "|#{i}|#{i.to_s(16).rjust(pad, '0')}|" }
  joined = ret.join("\n") + "\n"
  joined.justify_table(:right)
end

.to_oct_html_table(from = 1, to = 10) ⇒ Object

return is oct html table

Examples

65535 to 65536 case

Numeric.to_oct_html_table(65535, 65536)

result

<table>
  <tr>
    <th>10digit</th>
    <th>8digit</th>
  </tr>
  <tr>
    <td>65535</td>
    <td>177777</td>
  </tr>
  <tr>
    <td>65536</td>
    <td>200000</td>
  </tr>
</table>


30
31
32
33
34
35
36
# File 'lib/open_classes/numeric/to_oct_html_table.rb', line 30

def self.to_oct_html_table(from = 1, to = 10)
  ret = []
  size = to.to_s(8).size
  ret << "<table>\n  <tr>\n    <th>10digit</th>\n    <th>8digit</th>\n  </tr>"
  (from..to).each { |i|ret << "  <tr>\n    <td>#{i}</td>\n    <td>#{i.to_s(8)}</td>\n  </tr>" }
  ret.join("\n") + "\n</table>\n"
end

.to_oct_table(from = 1, to = 10) ⇒ Object

return is oct table

Examples

65535 to 65536 case

Numeric.to_oct_table(65535, 65536)

result

|10digit|8digit|
|  65535|177777|
|  65536|200000|


19
20
21
22
23
24
25
26
# File 'lib/open_classes/numeric/to_oct_table.rb', line 19

def self.to_oct_table(from = 1, to = 10)
  ret = []
  size = to.to_s(8).size
  ret << '|10digit|8digit|'
  (from..to).each { |i|ret << "|#{i}|#{i.to_s(8).rjust(size, '0')}|" }
  joined = ret.join("\n") + "\n"
  joined.justify_table(:right)
end

Instance Method Details

#dice_backObject

return dice back number

Examples

each 1-6 case

1.dice_back # => return 6
2.dice_back # => return 5
3.dice_back # => return 4
4.dice_back # => return 3
5.dice_back # => return 2
6.dice_back # => return 1

other case

7.dice_back # => return 7


25
26
27
# File 'lib/open_classes/numeric/dice_back.rb', line 25

def dice_back
  DICE.include?(self) ? DICE[self] : self
end

#dozenObject

get dozen number

Examples

0,1,2 case

0.dozen # => return 0
1.dozen # => return 12
2.dozen # => return 24


15
16
17
18
# File 'lib/open_classes/numeric/dozen.rb', line 15

def dozen
  return 0 if self == 0
  self * 12
end

#is_ascii?Boolean

return is ascii or not

Examples

1,127,128 case

1.is_ascii? # => return true
127.is_ascii? # => return true
128.is_ascii? # => return false

Returns:

  • (Boolean)


15
16
17
# File 'lib/open_classes/numeric/is_ascii.rb', line 15

def is_ascii?
  self < 128
end