Class: String

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

Overview

##

This extension adds utility methods to the String class.

##

Instance Method Summary collapse

Instance Method Details

#titleize_customObject

##

Provide a custom titleize method to handle strings especially ones with hyphens more appropriately. Standard titleize does not preserve hyphens in the desired manner.

Reference: stackoverflow.com/questions/29784873/titleize-a-hyphenated-name

Examples: “SANT-ANDREA-D’ORCINO”.titleize_custom => Sant-Andrea-D’orcino “SANT-ANDREA-D’ORCINO”.titleize => Sant Andrea D’orcino

##


38
39
40
# File 'lib/immosquare-extensions/string.rb', line 38

def titleize_custom
  humanize.gsub(/\b('?[a-z])/) { ::Regexp.last_match(1).capitalize }
end

#to_booleanObject

##

Convert the string ‘true’ to true and ‘false’ to false. Any other value will return nil.

Examples: “true”.to_boolean => true “false”.to_boolean => false “random”.to_boolean => nil

##


17
18
19
20
21
22
23
24
# File 'lib/immosquare-extensions/string.rb', line 17

def to_boolean
  case downcase
  when "true"
    true
  when "false"
    false
  end
end

#upcaseObject

##

Overriding the standard upcase method to provide a more comprehensive version that correctly handles Unicode characters.

Example: “José”.upcase => “JOSÉ”

##


49
50
51
# File 'lib/immosquare-extensions/string.rb', line 49

def upcase
  UnicodeUtils.upcase(self)
end