Class: String

Inherits:
Object show all
Defined in:
lib/activesupport/camelize.rb,
lib/activesupport/blank.rb

Overview

rubocop:disable all

Instance Method Summary collapse

Instance Method Details

#blank?true, false

A string is blank if it’s empty or contains whitespaces only:

''.blank?       # => true
'   '.blank?    # => true
"\t\n\r".blank? # => true
' blah '.blank? # => false

Unicode whitespace is supported:

"\u00a0".blank? # => true

Returns:

  • (true, false)


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/activesupport/blank.rb', line 120

def blank?
  # The regexp that matches blank strings is expensive. For the case of empty
  # strings we can speed up this method (~3.5x) with an empty? call. The
  # penalty for the rest of strings is marginal.
  empty? ||
    begin
      BLANK_RE.match?(self)
    rescue Encoding::CompatibilityError
      false
    end
end

#camelizeObject

Converts strings to UpperCamelCase.

Also converts ‘/’ to ‘::’ which is useful for converting paths to namespaces.

camelize('active_model')                # => "ActiveModel"
camelize('active_model/errors')         # => "ActiveModel::Errors"

Modified copy of ActiveSupport::Inflector.camelize. Since I did not need those features, I removed the handling of the :lower parameter and the handling of acronyms. github.com/rails/rails/blob/v6.1.3.1/activesupport/lib/active_support/inflector/methods.rb



17
18
19
20
21
22
23
24
25
26
# File 'lib/activesupport/camelize.rb', line 17

def camelize
  string = to_s
  string = string.sub(/^[a-z\d]*/) { |match| match.capitalize! || match }
  string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) do
    word        = Regexp.last_match(2)
    substituted = word.capitalize! || word
    Regexp.last_match(1) ? "::#{substituted}" : substituted
  end
  string
end

#constantizeObject



28
29
30
# File 'lib/activesupport/camelize.rb', line 28

def constantize
  Object.const_get(self)
end