Class: String

Inherits:
Object show all
Defined in:
lib/runtime/mixins.rb

Overview

The String class

Constant Summary collapse

DefaultUnderscoreOptions =
{
  downcase: true
}.freeze
HumanUnits =
{ b: 'bytes', kb: 'kilobytes', mb: 'megabytes', gb: 'gigabytes', tb: 'terabytes' }.freeze
HumanUnitsRegex =
/(#{HumanUnits.keys.join('|')})/i.freeze

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object



324
325
326
# File 'lib/runtime/mixins.rb', line 324

def +(other)
  format(JoinedTemplate, str: self, other: other)
end

#camelize(_s = nil) ⇒ Object



384
385
386
387
388
# File 'lib/runtime/mixins.rb', line 384

def camelize(_s = nil)
  self.downcase.split(/[^a-z0-9]/).inject('') do |str1, str2|
    str2.length > 1 ? [str1, str2[0].chr.upcase, str2[1..]].join : str1.to_s
  end
end

#dequoteObject



390
391
392
# File 'lib/runtime/mixins.rb', line 390

def dequote
  self.gsub(/(\A['"]|['"]\Z)/, '')
end

#midsub(regexp, &_block) ⇒ Object

Replace the second of three capture groups with the given block.



329
330
331
# File 'lib/runtime/mixins.rb', line 329

def midsub(regexp, &_block)
  self.gsub(regexp) { '\1' + yield('\2') + '\3' }
end

#poststripObject



337
338
339
# File 'lib/runtime/mixins.rb', line 337

def poststrip
  self.gsub(/\s+$/, '')
end

#poststrip!Object



341
342
343
# File 'lib/runtime/mixins.rb', line 341

def poststrip!
  self.gsub!(/\s+$/, '')
end

#prestripObject



345
346
347
# File 'lib/runtime/mixins.rb', line 345

def prestrip
  self.gsub(/^\s+/, '')
end

#prestrip!Object



349
350
351
# File 'lib/runtime/mixins.rb', line 349

def prestrip!
  self.gsub!(/^\s+/, '')
end

#save(file) ⇒ Object



394
395
396
# File 'lib/runtime/mixins.rb', line 394

def save(file)
  File.write(file, self)
end

#sentence_caseObject



333
334
335
# File 'lib/runtime/mixins.rb', line 333

def sentence_case
  self.capitalize
end

#snake_caseObject



371
372
373
374
375
376
377
378
# File 'lib/runtime/mixins.rb', line 371

def snake_case
  self
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    .gsub(/([a-z\d])([A-Z])/,'\1_\2')
    .tr(' -', '_')
    .downcase
end

#titleizeObject



380
381
382
# File 'lib/runtime/mixins.rb', line 380

def titleize
  self.split(/[\W_]/).map(&:capitalize).join(' ')
end

#to_humanObject



401
402
403
404
405
# File 'lib/runtime/mixins.rb', line 401

def to_human
  scalar, unit = self.split
  unit.sub!(HumanUnitsRegex) { |s| HumanUnits[s.downcase.to_sym] }
  [scalar.to_f.commas, unit].join(' ')
end

#to_nObject



353
354
355
# File 'lib/runtime/mixins.rb', line 353

def to_n
  /\./.match?(self) ? self.to_f : self.to_i
end

#underscore(options = {}) ⇒ Object



361
362
363
364
365
366
367
368
369
# File 'lib/runtime/mixins.rb', line 361

def underscore(options = {})
  options = DefaultUnderscoreOptions.merge(options)
  return snake_case if options[:downcase]
  self
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    .gsub(/([a-z\d])([A-Z])/,'\1_\2')
    .tr(' -', '_')
end