Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/rcore_ext/string/format.rb,
lib/rcore_ext/string/numeric.rb

Constant Summary collapse

@@common_formats =
{bin: 'B*', hex: 'H*'}

Instance Method Summary collapse

Instance Method Details

#decode_string(format, options = {}) ⇒ Object

Decode string with various formats. Supported formats: :bin, :hex, :base32, :base64

You can also pass options for Base64 to use

  • :strict

  • :url_safe

    “hello world!”.decode_string(:hex) #=> 68656c6c6f20776f726c6421 “hi”.decode_string(:bin) #=> 0110100001101001

    “NBSWY3DPEB3W64TMMQQQ====”.decode_string(:base32) #=> hello world! “aGVsbG8gd29ybGQhn”.decode_string(:base64) #=> hello world!

    “aGVsbG8gd29ybGQh”.decode_string(:base64, strict: true) #=> hello world!

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rcore_ext/string/format.rb', line 18

def decode_string(format, options={})
  raise ArgumentError, "Format can't be empty" if format.nil? 
  format = format.to_sym    
  @options = options || {}

  case format
  when :base32
    base32_decode
  when :base64
    base64_decode
  else
    raise ArgumentError, "Unsupported format: #{format}" unless @@common_formats.has_key?(format)
    common_decode(format)
  end
end

#encode_string(format, options = {}) ⇒ Object

Encode string with various formats. Supported formats: :bin, :hex, :base32, :base64

You can also pass options for Base64 to use

  • :strict

  • :url_safe

    “68656c6c6f20776f726c6421”.decode_string(:hex) #=> hello world! “0110100001101001”.decode_string(:bin) #=> hi

    “hello world!”.decode_string(:base32) #=> NBSWY3DPEB3W64TMMQQQ==== “hello world!”.decode_string(:base64) #=> aGVsbG8gd29ybGQhn

    “hello world!”.decode_string(:base64, strict: true) #=> aGVsbG8gd29ybGQh

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rcore_ext/string/format.rb', line 48

def encode_string(format, options={})
  raise ArgumentError, "Format can't be empty" if format.nil?
  format = format.to_sym
  @options = options || {}

  case format
  when :base32
    base32_encode
  when :base64
    base64_encode
  else
    raise ArgumentError, "Unsupported format: #{format}" unless @@common_formats.has_key?(format)
    common_encode(format)
  end
end

#is_float?Boolean

Checks a string on a Float value

"1".is_float?             #=> false
"1.0".is_float?           #=> true
"not_numeric".is_float?   #=> false

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/rcore_ext/string/numeric.rb', line 25

def is_float?
  return is_numeric? unless is_integer?
  false
end

#is_integer?Boolean

Checks a string on an Integer value

"1".is_integer?             #=> true
"1.0".is_integer?           #=> false
"not_numeric".is_integer?   #=> false

Returns:

  • (Boolean)


7
8
9
# File 'lib/rcore_ext/string/numeric.rb', line 7

def is_integer?
  true if Integer(self) rescue false
end

#is_numeric?Boolean

Checks a string on a Numeric value

"1".is_numeric?             #=> true
"1.0".is_numeric?           #=> true
"not_numeric".is_numeric?   #=> false

Returns:

  • (Boolean)


16
17
18
# File 'lib/rcore_ext/string/numeric.rb', line 16

def is_numeric?
  true if Float(self) rescue false
end

#to_numericObject

Converts a string to a Numeric values (Float, Fixnum or Bignum).

"1".to_numeric                #=> 1
"1".to_numeric.class          #=> Fixnum
"1.0".to_numeric              #=> 1.0
"1.0".to_numeric.class        #=> Float
("1" * 10).to_numeric         #=> 1111111111
("1" * 10).to_numeric.class   #=> Bignum
"not_numeric".to_numeric      #=> nil


39
40
41
42
# File 'lib/rcore_ext/string/numeric.rb', line 39

def to_numeric
  return Integer(self)  if is_integer?
  return Float(self)    if is_float?
end