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

##


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

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

#to_boolean(default_value = nil) ⇒ Object

##

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 “random”.to_boolean(true) => true

##


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

def to_boolean(default_value = nil)
  case downcase
  when "true"
    true
  when "false"
    false
  else
    default_value
  end
end

#upcaseObject

##

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

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

##


52
53
54
# File 'lib/immosquare-extensions/string.rb', line 52

def upcase
  UnicodeUtils.upcase(self)
end