Method: Dry::Inflector#humanize

Defined in:
lib/dry/inflector.rb

#humanize(input) ⇒ String

Humanize a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.humanize("dry_inflector") # => "Dry inflector"
inflector.humanize("author_id")     # => "Author"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the humanized string

Since:

  • 0.1.0



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/dry/inflector.rb', line 153

def humanize(input)
  input = input.to_s
  result = inflections.humans.apply_to(input)
  result.delete_suffix!("_id")
  result.tr!("_", " ")
  match = /([^[:alnum:]])/.match(result)
  separator = match ? match[0] : DEFAULT_SEPARATOR
  result.split(separator).map.with_index { |word, index|
    inflections.acronyms.apply_to(word, capitalize: index.zero?)
  }.join(separator)
end