Class: String

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

Instance Method Summary collapse

Instance Method Details

#%(arg) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ext/string.rb', line 25

def %(arg)
  if ::RUBY_VERSION <= "1.8.7" && arg.is_a?(Hash)
    return gsub(/%\{(\w+)\}/) do |s|
      if arg.has_key?($1.to_sym)
        arg[$1.to_sym]
      else
        raise KeyError.new("key{#{$1}} not found")
      end
    end
  elsif arg.is_a?(Array)
    return Kernel.sprintf(self, *arg)
  else
    return Kernel.sprintf(self, arg)
  end
end

#chunk(max_length) ⇒ Object



20
21
22
23
# File 'lib/ext/string.rb', line 20

def chunk(max_length)
  type_assert(max_length, Integer)
  chars.each_slice(max_length).to_a.map! { |ary| ary.join("") }
end

#colorized?Boolean

Returns:



6
7
8
# File 'lib/ext/string.rb', line 6

def colorized?
  self =~ /\e\[0[;0-9]*m/ && true || false
end

#to_constObject



51
52
53
# File 'lib/ext/string.rb', line 51

def to_const
  split("::").inject(::Object) { |k,c| k.const_get(c) }
end

#to_dateObject

Transform self to a Date



42
43
44
# File 'lib/ext/string.rb', line 42

def to_date
  Date.parse(self)
end

#to_timeObject

Transform self to a Time



47
48
49
# File 'lib/ext/string.rb', line 47

def to_time
  Time.parse(self)
end

#uncolorizeObject



2
3
4
# File 'lib/ext/string.rb', line 2

def uncolorize
  self.gsub(/\e\[0[;0-9]*m/, "")
end

#word_wrap(width = (tty_width || 80).to_i) ⇒ Object



10
11
12
13
14
# File 'lib/ext/string.rb', line 10

def word_wrap(width=(tty_width || 80).to_i)
  ret = dup
  ret.word_wrap!(width)
  ret
end

#word_wrap!(width = (tty_width || 80).to_i) ⇒ Object



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

def word_wrap!(width=(tty_width || 80).to_i)
  self.gsub!(/(.{1,#{width}})( +|$\n?)|(.{1,#{width}})/, "\\1\\3\n")
end