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
- #blue ⇒ Object
-
#bold ⇒ Object
Add ability to output colored text to console e.g.: puts “Hello”.red.
- #camelize(first_letter = :upper) ⇒ Object
- #colorize(color_code) ⇒ Object
- #dark_blue ⇒ Object
- #dark_green ⇒ Object
- #dark_red ⇒ Object
- #green ⇒ Object
- #grey ⇒ Object
- #pur ⇒ Object
- #red ⇒ Object
-
#snake_case ⇒ String
Convert to snake case.
-
#to_const ⇒ Constant
Convert constant name to constant.
-
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
- #yellow ⇒ Object
Instance Method Details
#blue ⇒ Object
106 |
# File 'lib/right_support/ruby/string_extensions.rb', line 106 def blue; colorize("\e[1m\e[34m"); end |
#bold ⇒ Object
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_blue ⇒ Object
107 |
# File 'lib/right_support/ruby/string_extensions.rb', line 107 def dark_blue; colorize("\e[34m"); end |
#dark_green ⇒ Object
104 |
# File 'lib/right_support/ruby/string_extensions.rb', line 104 def dark_green; colorize("\e[32m"); end |
#dark_red ⇒ Object
102 |
# File 'lib/right_support/ruby/string_extensions.rb', line 102 def dark_red; colorize("\e[31m"); end |
#green ⇒ Object
103 |
# File 'lib/right_support/ruby/string_extensions.rb', line 103 def green; colorize("\e[1m\e[32m"); end |
#grey ⇒ Object
100 |
# File 'lib/right_support/ruby/string_extensions.rb', line 100 def grey; colorize("\e[30m"); end |
#pur ⇒ Object
108 |
# File 'lib/right_support/ruby/string_extensions.rb', line 108 def pur; colorize("\e[1m\e[35m"); end |
#red ⇒ Object
101 |
# File 'lib/right_support/ruby/string_extensions.rb', line 101 def red; colorize("\e[1m\e[31m"); end |
#snake_case ⇒ String
Convert to snake case.
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
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_const ⇒ Constant
Convert constant name to constant
"FooBar::Baz".to_const => FooBar::Baz
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_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
"FooBar::Baz".to_const_path # => "foo_bar/baz"
57 58 59 |
# File 'lib/right_support/ruby/string_extensions.rb', line 57 def to_const_path snake_case.gsub(/::/, "/") end |
#yellow ⇒ Object
105 |
# File 'lib/right_support/ruby/string_extensions.rb', line 105 def yellow; colorize("\e[1m\e[33m"); end |