Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/under_os/core/string.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/under_os/core/string.rb', line 26

def blank?
  self !~ /[^[:space:]]/
end

#camelizeObject



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

def camelize
  gsub(/(\-|_)+([a-z])?/){|m| $2 ? $2.upcase : ''}
end

#capitalizeObject



14
15
16
# File 'lib/under_os/core/string.rb', line 14

def capitalize
  self[0].upcase + slice(1, size)
end

#constantizeObject



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

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

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#dasherizeObject



10
11
12
# File 'lib/under_os/core/string.rb', line 10

def dasherize
  underscore.gsub('_', '-')
end

#ends_with?(substr) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/under_os/core/string.rb', line 22

def ends_with?(substr)
  rindex(substr)  == size - substr.size
end

#starts_with?(substr) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/under_os/core/string.rb', line 18

def starts_with?(substr)
  index(substr) == 0
end

#underscoreObject



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

def underscore
  gsub(/([a-z\d])([A-Z]+)/, '\1_\2').gsub('-', '_').downcase
end