Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/thefox-ext/ext/string.rb
Instance Method Summary collapse
-
#is_digit? ⇒ Boolean
Is a String only made of numbers?.
-
#is_lower? ⇒ Boolean
Is a String only made of lower-case charaters.
-
#is_upper? ⇒ Boolean
Is a String only made of upper-case charaters.
- #is_utf8? ⇒ Boolean
-
#resolve_range(prefix = '') ⇒ Object
Resolve a range string to an array.
-
#titlecase ⇒ Object
Convert ‘hello world’ to ‘Hello World’.
- #to_hex ⇒ Object
-
#to_i32a ⇒ Object
Convert a String to an Integer 32-bit Array.
- #to_utf8 ⇒ Object
Instance Method Details
#is_digit? ⇒ Boolean
Is a String only made of numbers?
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.
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.
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
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 |
#resolve_range(prefix = '') ⇒ Object
Resolve a range string to an array. A range string can be like ‘1, 3..5, 9-11, 12+, 14++, 17+++’. Which will be resolved to [1, 3, 4, 5, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20].
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/thefox-ext/ext/string.rb', line 70 def resolve_range(prefix = '') # puts '-> resolve_range: %s {%s}' % [self, prefix] rv = Array.new items1 = self.split(',') # pp items1 items2 = [] is_sub_range = false sub_item = [] items1.each do |item| if item.count('{') > 0 is_sub_range = true end if is_sub_range sub_item.push(item) else items2.push(item) end if item.count('}') > 0 is_sub_range = false items2.push(sub_item.join(',')) sub_item = [] end end # pp items2 items2.map{ |item| item_striped = item.strip if range_match = item_striped.match(/(\d+)\{([\d\-\+,]+)\}/) range_match[2].resolve_range(range_match[1]) elsif /\.\./.match(item_striped) # ( . )( . ) <--- BOOBS Range.new(*item_striped.split('..', 2).map{ |range| range.to_i }) elsif /-/.match(item_striped) Range.new(*item_striped.split('-', 2).map{ |range| range.to_i }) elsif /\+/.match(item_striped) items = item_striped.split('+') range_begin = items[0].to_i range_end = range_begin + item_striped.count('+') Range.new(range_begin, range_end) else item_striped.to_i end }.each{ |range| if range.is_a?(Range) rv.push(*range.to_a) elsif range.is_a?(Array) rv.push(*range) else rv << range end } if !prefix.empty? rv = rv.map { |i| (prefix + i.to_s).to_i } end rv end |
#titlecase ⇒ Object
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_hex ⇒ Object
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_i32a ⇒ Object
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_utf8 ⇒ Object
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 |