Module: RightSupport::Ruby::StringExtensions

Included in:
String
Defined in:
lib/right_support/ruby/string_extensions.rb

Constant Summary collapse

ACTIVE_SUPPORT_WORKALIKES =
true

Instance Method Summary collapse

Instance Method Details

#blueObject



106
# File 'lib/right_support/ruby/string_extensions.rb', line 106

def blue; colorize("\e[1m\e[34m"); end

#boldObject

Add ability to output colored text to console e.g.: puts “Hello”.red



99
# File 'lib/right_support/ruby/string_extensions.rb', line 99

def bold; colorize("\e[1m\e[29m"); end

#camelize(first_letter = :upper) ⇒ Object



89
90
91
92
93
94
# File 'lib/right_support/ruby/string_extensions.rb', line 89

def camelize(first_letter = :upper)
  case first_letter
    when :upper then gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
    when :lower then first + camelize(self)[1..-1]
  end
end

#colorize(color_code) ⇒ Object



109
110
111
112
113
# File 'lib/right_support/ruby/string_extensions.rb', line 109

def colorize(color_code)
  # Doesn't work with the Windows prompt...
  @@windows ||= RbConfig::CONFIG['host_os'] =~ /mswin|win32|dos|mingw|cygwin/i
  (@@windows || !$stdout.isatty) ? to_s : "#{color_code}#{to_s}\e[0m"
end

#dark_blueObject



107
# File 'lib/right_support/ruby/string_extensions.rb', line 107

def dark_blue; colorize("\e[34m"); end

#dark_greenObject



104
# File 'lib/right_support/ruby/string_extensions.rb', line 104

def dark_green; colorize("\e[32m"); end

#dark_redObject



102
# File 'lib/right_support/ruby/string_extensions.rb', line 102

def dark_red; colorize("\e[31m"); end

#greenObject



103
# File 'lib/right_support/ruby/string_extensions.rb', line 103

def green; colorize("\e[1m\e[32m"); end

#greyObject



100
# File 'lib/right_support/ruby/string_extensions.rb', line 100

def grey; colorize("\e[30m"); end

#purObject



108
# File 'lib/right_support/ruby/string_extensions.rb', line 108

def pur; colorize("\e[1m\e[35m"); end

#redObject



101
# File 'lib/right_support/ruby/string_extensions.rb', line 101

def red; colorize("\e[1m\e[31m"); end

#snake_caseString

Convert to snake case.

"FooBar".snake_case           #=> "foo_bar"
"HeadlineCNNNews".snake_case  #=> "headline_cnn_news"
"CNN".snake_case              #=> "cnn"

Returns:

  • (String)

    Receiver converted to snake case.



42
43
44
45
46
47
# File 'lib/right_support/ruby/string_extensions.rb', line 42

def snake_case
  return downcase if match(/\A[A-Z]+\z/)
  gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
  gsub(/([a-z])([A-Z])/, '\1_\2').
  downcase
end

#to_constConstant

Convert constant name to constant

"FooBar::Baz".to_const => FooBar::Baz

Returns:

  • (Constant)

    Constant corresponding to given name or nil if no constant with that name exists



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/right_support/ruby/string_extensions.rb', line 69

def to_const
  names = split('::')
  names.shift if names.empty? || names.first.empty?
  
  constant = Object
  names.each do |name|
    # modified to return nil instead of raising an const_missing error
    constant = constant && constant.const_defined?(name) ? constant.const_get(name) : nil
  end
  constant
end

#to_const_pathString

Convert a constant name to a path, assuming a conventional structure.

"FooBar::Baz".to_const_path # => "foo_bar/baz"

Returns:

  • (String)

    Path to the file containing the constant named by receiver (constantized string), assuming a conventional structure.



57
58
59
# File 'lib/right_support/ruby/string_extensions.rb', line 57

def to_const_path
  snake_case.gsub(/::/, "/")
end

#yellowObject



105
# File 'lib/right_support/ruby/string_extensions.rb', line 105

def yellow; colorize("\e[1m\e[33m"); end