Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ipxact/ruby_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#baseFixnum

Returns base of number represented by this string e.g. ‘0x0F’ => 16

Returns:

  • (Fixnum)

    returns base of number represented by this string e.g. ‘0x0F’ => 16



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ipxact/ruby_ext/string.rb', line 11

def base
  case self
    when PREFIX_BIN, POSTFIX_BIN
      2
    when PREFIX_HEX, POSTFIX_HEX, NAKED_HEX
      16
    when DECIMAL
      10
    else
      nil
  end
end

#to_decFixnum

Returns numerical value of string in decimal form

Returns:

  • (Fixnum)

    returns numerical value of string in decimal form



25
26
27
28
29
30
31
32
33
# File 'lib/ipxact/ruby_ext/string.rb', line 25

def to_dec
  case self
    when PREFIX_BIN, PREFIX_HEX   then self[2..-1]
    when POSTFIX_HEX, POSTFIX_BIN then self[0..-2]
    when DECIMAL, NAKED_HEX       then self
    else
      raise Exception, "'#{self}' is not a numerical string!"
  end.to_i(base)
end