Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ecraft/extensions/string.rb

Instance Method Summary collapse

Instance Method Details

#to_boolObject



41
42
43
# File 'lib/ecraft/extensions/string.rb', line 41

def to_bool
  !self !~ /^(true|t|yes|y|1)$/i
end

#to_camelcaseObject

require 'active_support' require 'active_support/core_ext/string/inflections'

'active_support_string'.camelize # result: "ActiveSupportString"



9
10
11
12
13
14
15
16
17
# File 'lib/ecraft/extensions/string.rb', line 9

def to_camelcase
  separators = ['_', '\s']
  str = dup
  separators.each do |s|
    str = str.gsub(/(?:#{s}+)([a-z])/) { Regexp.last_match(1).upcase }
  end
  str = str.gsub(/(\A|\s)([a-z])/) { Regexp.last_match(1) + Regexp.last_match(2).upcase }
  str
end

#to_classObject

require 'active_support' require 'active_support/core_ext/string/inflections' 'Foo::Bar'.constantize # => Foo::Bar



30
31
32
33
34
35
36
37
38
39
# File 'lib/ecraft/extensions/string.rb', line 30

def to_class
  chain = split '::'
  klass = Kernel
  chain.each do |klass_string|
    klass = klass.const_get klass_string
  end
  klass.is_a?(Class) ? klass : nil
rescue NameError
  nil
end

#to_snake_caseObject

require 'active_support' require 'active_support/core_ext/string/inflections'

'ActiveSupportString'.underscore # result: "active_support_string"



23
24
25
# File 'lib/ecraft/extensions/string.rb', line 23

def to_snake_case
  gsub(/\B[A-Z]/, '_\&').downcase
end