Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ruuuby/extensions.rb

Overview

Add extensions to the default class(String).

Instance Method Summary collapse

Instance Method Details

#ensure_ending!(ending) ⇒ String

Parameters:

Returns:

Raises:

  • (ArgumentError)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ruuuby/extensions.rb', line 184

def ensure_ending!(ending)
  Ruuuby::Params::check_string(ending)
  return self if ending == '' || self.end_with?(ending)
  len_this = self.length
  return self << ending if len_this == 0
  last_matched = ''
  len_ending   = ending.length
  delta        = 0
  while delta <= len_this && delta <= len_ending
    ending_of_this  = self[(len_this-1-delta)..(len_this-1)]
    starting_of_end = ending[0..delta]
    last_matched    = starting_of_end if ending_of_this == starting_of_end
    delta          += 1
  end
  self << (last_matched == '' ? ending : ending[last_matched.length..len_ending-1])
end

#ensure_ending_char!(ending) ⇒ String

Parameters:

Returns:

Raises:

  • (ArgumentError)


206
207
208
209
210
# File 'lib/ruuuby/extensions.rb', line 206

def ensure_ending_char!(ending)
  Ruuuby::Params::check_char(ending)
  self << ending if self.length == 0 || self[-1] != ending
  self
end