Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#camel_casedString

Returns a camel cased version of a String.

Examples:

“foo_bar_baz”.camel_cased # => “FooBarBaz”

Returns:

  • (String)

    Camel cased version of ‘self`.



17
18
19
# File 'lib/core_ext/string.rb', line 17

def camel_cased
  split("_").map(&:capitalize).join
end

#capitalized?Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/core_ext/string.rb', line 28

def capitalized?
  c = self[0]
  c && c.upcase == c
end

#snake_casedString

Returns a snake cases version of a String.

Examples:

“FooBarBaz”.snake_cased # => “foo_bar_baz”

Returns:

  • (String)

    Snake cased version of ‘self`.



6
7
8
9
10
11
# File 'lib/core_ext/string.rb', line 6

def snake_cased
  r1 = /([A-Z]+)([A-Z][a-z])/
  r2 = /([a-z\d])([A-Z])/

  gsub(r1,'\1_\2').gsub(r2,'\1_\2').tr("-", "_").downcase
end

#without_trailing_whitespaceString

Returns ‘self` with all trailing whitespace removed.

Returns:

  • (String)

    ‘self` without any trailing whitespace.



24
25
26
# File 'lib/core_ext/string.rb', line 24

def without_trailing_whitespace
  lines.map(&:rstrip).join("\n")
end