Class: String

Inherits:
Object show all
Defined in:
lib/corelib/string/core.rb

Instance Method Summary collapse

Instance Method Details

#combine(*args) ⇒ Object

TODO - Needs Tests Combines two strings together with a separator.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/corelib/string/core.rb', line 15

def combine(*args)
  options = args.extract_options!
  raise ArgumentError, "You need to supply at least one string" if args.empty?
  str = self
  args.each { |val| str = str.priv_combine(val, options) }

  return options.fetch(:if_empty, "") if str.blank?

  prefix = options.fetch(:prefix, nil)
  str = "#{prefix}#{str}"  if options.fetch(:wrap, "true") and (prefix.not_nil?)
  suffix = options.fetch(:suffix, nil)
  str = "#{str}#{suffix}" if options.fetch(:wrap, "true") and (suffix.not_nil?)
  str
end

#concat_with(str, separator = "") ⇒ Object

Does the same thing as String#contact, but allows a separator to be inserted between the two strings.



32
33
34
35
36
37
# File 'lib/corelib/string/core.rb', line 32

def concat_with(str, separator="")
  return self if str.nil? or str.empty?
  return self.concat(str) if self.empty?
  self.concat(separator) unless separator.empty?
  self.concat(str)
end

#excerpt_to_end_of_word(position = nil) ⇒ Object

Returns the subset of a string from [0, position] if string is a space. If string is not a space, it is assumed we are in the middle of a word. and the logic will increase position a little bit to not break in the middle of a word.



68
69
70
71
72
73
74
75
# File 'lib/corelib/string/core.rb', line 68

def excerpt_to_end_of_word(position=nil)
   return self if position.nil? or position >= self.size

   char = self[position]
   return self[0, position].rstrip if char == " "

   self[0,index_of_next_space_from(position)].rstrip
end

#firstObject

TODO - Needs Tests



9
10
11
# File 'lib/corelib/string/core.rb', line 9

def first
  self.empty? ? nil : self[0,1]
end

#index_of_next_space_from(position) ⇒ Object

Given a position, return the position of the next space



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/corelib/string/core.rb', line 78

def index_of_next_space_from(position)
  return nil if self.empty? or position.nil?
  return nil if position >= self.size

  idx = position
  (self.size - position).times do
    idx = idx + 1
    return idx if self[idx] == " "
  end
  idx
end

#lastObject

TODO - Needs Tests



4
5
6
# File 'lib/corelib/string/core.rb', line 4

def last
  self.empty? ? nil : self[-1,1]
end

#not_empty?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/corelib/string/core.rb', line 61

def not_empty?
  !self.empty?
end

#to_bool(options = {}) ⇒ Object

true will always be returned if we can clearly match one of the true cases In unstrict mode, the string is assumed false if we cannot match true In strict mode, the string must clearly match a false condition to return false otherise an error is raised



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/corelib/string/core.rb', line 47

def to_bool(options={})
  strip = options.fetch(:strip, true)
  strict = options.fetch(:strict, false)
  str = strip ? self.strip : self
  return true if str =~ /\A(true|t|yes|y|1)\Z/i

  if strict
    return false if str.empty? || str =~ /\A(false|f|no|n|0)\Z/i
    raise ArgumentError.new("cannot convert \"#{str}\" to boolean")
  end

  false
end

#to_yes_no(options = {}) ⇒ Object



39
40
41
# File 'lib/corelib/string/core.rb', line 39

def to_yes_no(options={})
  self.to_bool(options).to_yes_no(options)
end