Class: String

Inherits:
Object show all
Defined in:
lib/core_ext/blank.rb,
lib/core_ext/snake_case.rb
more...

Constant Summary collapse

NON_WHITESPACE_REGEXP =

0x3000: fullwidth whitespace

%r![^\s#{[0x3000].pack("U")}]!

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

A string is blank if it’s empty or contains whitespaces only:

"".blank?                 # => true
"   ".blank?              # => true
" ".blank?               # => true
" something here ".blank? # => false

Returns:

  • (Boolean)
[View source]

103
104
105
# File 'lib/core_ext/blank.rb', line 103

def blank?
  self !~ NON_WHITESPACE_REGEXP
end

#to_snake_caseObject

[View source]

10
11
12
# File 'lib/core_ext/snake_case.rb', line 10

def to_snake_case
  dup.tap { |s| s.to_snake_case! }
end

#to_snake_case!Object

ruby mutation methods have the expectation to return self if a mutation occurred, nil otherwise. (see www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub-21)

[View source]

5
6
7
8
# File 'lib/core_ext/snake_case.rb', line 5

def to_snake_case!
  gsub!(/(.)([A-Z])/,'\1_\2')
  downcase!
end