Class: String

Inherits:
Object show all
Defined in:
lib/awsbase/support.rb,
lib/awsbase/support.rb,
lib/cmeiklejohn/string.rb

Overview

Hack to add in 1.8.6 support for ruby. Doesn’t work in 1.8.6 unless active support is loaded, since each_char isn’t available until 1.8.7

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/awsbase/support.rb', line 109

def blank?
    empty? || strip.empty?
end

#constantizeObject

Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

Examples

"Module".constantize #=> Module
"Class".constantize #=> Class


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/awsbase/support.rb', line 42

def constantize()
    camel_cased_word = self
    names = camel_cased_word.split('::')
    names.shift if names.empty? || names.first.empty?

    constant = Object
    names.each do |name|
      if Module.method(:const_get).arity == 1
        constant = constant.const_get(name) || constant.const_missing(name)
      else
        constant = constant.const_get(name, false) || constant.const_missing(name)
      end
    end
    constant
end

#each_charObject



8
9
10
11
12
13
14
15
16
# File 'lib/cmeiklejohn/string.rb', line 8

def each_char
   if block_given?
     scan(/./m) do |x|
       yield x
     end
   else
     scan(/./m)
   end
end

#linesObject



21
22
23
24
25
26
27
28
29
# File 'lib/cmeiklejohn/string.rb', line 21

def lines
  if block_given?
    each_line do |x|
      yield x
    end
  else
    to_a
  end
end