class String

Public Instance Methods

is_digit?() click to toggle source

Is a String only made of numbers?

# File lib/thefox-ext/ext/string.rb, line 5
def is_digit?
  r = '0'..'9'
  self.split('').keep_if{ |c| r.include?(c) }.count == self.length
end
is_lower?() click to toggle source

Is a String only made of lower-case charaters.

# File lib/thefox-ext/ext/string.rb, line 11
def is_lower?
  r = 'a'..'z'
  self.split('').keep_if{ |c| r.include?(c) }.count == self.length
end
is_upper?() click to toggle source

Is a String only made of upper-case charaters.

# File lib/thefox-ext/ext/string.rb, line 17
def is_upper?
  r = 'A'..'Z'
  self.split('').keep_if{ |c| r.include?(c) }.count == self.length
end
is_utf8?() click to toggle source
# File lib/thefox-ext/ext/string.rb, line 22
def is_utf8?
  begin
    self.unpack('U*')
  rescue
    return false
  end
  return true
end
titlecase() click to toggle source

Convert 'hello world' to 'Hello World'.

# File lib/thefox-ext/ext/string.rb, line 32
def titlecase
  self
    .split(/ /)
    .map{ |word| word.capitalize }
    .join(' ')
end
to_hex() click to toggle source
# File lib/thefox-ext/ext/string.rb, line 39
def to_hex
  self.split('').map{ |c| sprintf '%02x', c.ord }.join
end
to_i32a() click to toggle source

Convert a String to an Integer 32-bit Array.

# File lib/thefox-ext/ext/string.rb, line 44
def to_i32a
  len = self.length
  len_w = (len >> 2) + (len & 0x3).to_b.to_i

  out = (0..(len_w - 1)).map{ |n| [n, 0] }.to_h

  i = 0
  self.split('').each do |s|
    out[i >> 2] |= (s.ord << ((3 - (i & 0x3)) << 3))
    i += 1
  end

  out
end
to_utf8() click to toggle source
# File lib/thefox-ext/ext/string.rb, line 59
def to_utf8
  if is_utf8?
    self.force_encoding('UTF-8')
  else
    self.force_encoding('ISO-8859-1').encode('UTF-8')
  end
end