Module: Addr
- Includes:
- ConDuxml
- Defined in:
- lib/ipxact/addr.rb
Constant Summary collapse
- HEX_POSTFIX =
'h'
- HEX_PREFIX =
'0x'
- HEX_MAG_DIGS =
4
- HEX_SEP =
'_'
- DEC_POSTFIX =
'd'
- DEC_MAG_DIGS =
3
- DEC_SEP =
','
- BIN_POSTFIX =
'b'
- BIN_PREFIX =
'0b'
- BIN_MAG_DIGS =
4
- BIN_SEP =
' '
Instance Method Summary collapse
- #bit_range ⇒ Object
-
#to_bin(str, opts = {}) ⇒ String
String representation of hex number.
-
#to_dec(str, opts = {}) ⇒ String
Formatted string.
-
#to_hex(str, opts = {}) ⇒ String
String representation of hex number.
Instance Method Details
#bit_range ⇒ Object
23 24 25 |
# File 'lib/ipxact/addr.rb', line 23 def bit_range [position, position+width] end |
#to_bin(str, opts = {}) ⇒ String
Returns string representation of hex number.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ipxact/addr.rb', line 59 def to_bin(str, opts={}) s = str.to_dec.to_s(2) s = add_padding(s, 1) if opts[:pad] if opts[:sep] separator = opts[:sep] == true ? BIN_SEP : opts[:sep] s = add_separators(s, BIN_MAG_DIGS, separator) end if opts[:post] s += opts[:post] == true ? BIN_POSTFIX : opts[:post] end if opts[:pre] s = (opts[:pre] == true ? BIN_PREFIX : opts[:pre]) + s end s end |
#to_dec(str, opts = {}) ⇒ String
Returns formatted string.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ipxact/addr.rb', line 79 def to_dec(str, opts={}) s = str.to_dec.to_s s = add_padding(s, opts[:pad]) if opts[:pad] if opts[:sep] separator = opts[:sep] == true ? DEC_SEP : opts[:sep] s = add_separators(s, DEC_MAG_DIGS, separator) end if opts[:post] s += opts[:post] == true ? DEC_POSTFIX : opts[:post] end s end |
#to_hex(str, opts = {}) ⇒ String
Returns string representation of hex number.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ipxact/addr.rb', line 34 def to_hex(str, opts={}) s = str.to_dec.to_s(16).upcase s = add_padding(s, opts[:pad]) if opts[:pad] if opts[:sep] separator = opts[:sep] == true ? HEX_SEP : opts[:sep] s = add_separators(s, HEX_MAG_DIGS, separator) end if opts[:post] s += opts[:post] == true ? HEX_POSTFIX : opts[:post] end if opts[:pre] s = opts[:pre] == true ? HEX_PREFIX : opts[:pre] + s end s end |