Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/thefox-ext/ext/string.rb

Instance Method Summary collapse

Instance Method Details

#is_digit?Boolean

Is a String only made of numbers?

Returns:

  • (Boolean)


5
6
7
8
# 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?Boolean

Is a String only made of lower-case charaters.

Returns:

  • (Boolean)


11
12
13
14
# 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?Boolean

Is a String only made of upper-case charaters.

Returns:

  • (Boolean)


17
18
19
20
# 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?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
# File 'lib/thefox-ext/ext/string.rb', line 22

def is_utf8?
  begin
    self.unpack('U*')
  rescue
    return false
  end
  return true
end

#titlecaseObject

Convert ‘hello world’ to ‘Hello World’.



32
33
34
35
36
37
# File 'lib/thefox-ext/ext/string.rb', line 32

def titlecase
  self
    .split(/ /)
    .map{ |word| word.capitalize }
    .join(' ')
end

#to_hexObject



39
40
41
# File 'lib/thefox-ext/ext/string.rb', line 39

def to_hex
  self.split('').map{ |c| sprintf '%02x', c.ord }.join
end

#to_i32aObject

Convert a String to an Integer 32-bit Array.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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_utf8Object



59
60
61
62
63
64
65
# 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