Class: String

Inherits:
Object show all
Defined in:
lib/corelib_ruby/core_ext/string/core.rb,
lib/corelib_ruby/core_ext/string/queries.rb,
lib/corelib_ruby/core_ext/string/questions.rb,
lib/corelib_ruby/core_ext/string/conversions.rb

Instance Method Summary collapse

Instance Method Details

#cl_all_digits?(if_empty: false) ⇒ Boolean

Returns true if a non empty string contains all digit characters. Tabs, new lines, line feeds and whitespace all cause the method to return false.

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/corelib_ruby/core_ext/string/questions.rb', line 12

def cl_all_digits?(if_empty: false)
  return if_empty if empty?
  (self =~ /\A[0-9]+\z/).cl_not_nil?
end

#cl_all_letters?(if_empty: false) ⇒ Boolean

Returns true if a non empty string contains all letters. Tabs, new lines, line feeds and whitespace all cause the method to return false.

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/corelib_ruby/core_ext/string/questions.rb', line 19

def cl_all_letters?(if_empty: false)
  return if_empty if empty?
  (self =~ /\A[[:alpha:]]+\z/).cl_not_nil?
end

#cl_all_spaces?(when_empty: false) ⇒ Boolean

Returns true if a non empty string contains all space characters. Tabs and new lines are not considered spaces. Returns false for empty strings unless the when_empty: option is set to true.

Returns:

  • (Boolean)


4
5
6
7
8
# File 'lib/corelib_ruby/core_ext/string/questions.rb', line 4

def cl_all_spaces?(when_empty: false)
  return when_empty if empty?
  each_byte { |x| return false if x != 32 }
  true
end

#cl_combine(*args) ⇒ Object

Combines two strings together with a little more intelligence, using a separator. This methods reduces much of the prep you may need to do before concatenating two strings and allows you do do the concatenation on a single line. Examples

"me".cl_combine("you")  =>   "me you"
"  me     ".cl_combine("    you", separator: "-")  =>   "me-you"

Options

separator: The separate to place between the strings. (" " is the default)
prefix: A string that should appear in front of the final concatenation (no default)
suffix: A string that should appear at the end of the final concatenation (no default)
wrap: A boolean which helps control whether or not the prefix and suffix should be added (true is the default)
strip: Remove whitespace at the beginning and end of the strings before concatenating (true is the default)
if_empty: An alternative string to return if the resulting concatenation is empty.  This option is considered before the
          prefix or suffix is added.  ("" is the default)

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/corelib_ruby/core_ext/string/conversions.rb', line 53

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

  return options.fetch(:if_empty, "") if str.nil? || str.empty?

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

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

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



33
34
35
36
37
38
# File 'lib/corelib_ruby/core_ext/string/conversions.rb', line 33

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

#cl_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.



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

def cl_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, cl_index_of_next_space_from(position)].rstrip
end

#cl_firstObject



6
7
8
# File 'lib/corelib_ruby/core_ext/string/core.rb', line 6

def cl_first
  empty? ? "" : self[0, 1]
end

#cl_first_or_nilObject



10
11
12
# File 'lib/corelib_ruby/core_ext/string/core.rb', line 10

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

#cl_first_words(n = nil, ellipses: false) ⇒ Object

Retreive the fist n words of a string as a new string. Attach an optional ellipses what will display smartly

Examples

"This is a test".first_words(2)
# => "This is"
"This is a test".first_words(2, ellipses: true)
# => "This is ..."
"This is a test".first_words(2, ellipses: " +++")
# => "This is +++"


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

def cl_first_words(n=nil, ellipses: false)
  new_str = n.nil? ? self : split[0...n].join(' ')
  elip = cl_determine_ellipses(new_str, ellipses)
  new_str + "#{elip}"
end

#cl_force_leading_space(even_when_empty: false) ⇒ Object

Forces a string to have at least one leading space.



7
8
9
10
# File 'lib/corelib_ruby/core_ext/string/conversions.rb', line 7

def cl_force_leading_space(even_when_empty: false)
  (return even_when_empty ? " " : "") if empty?
  cl_all_spaces? ? dup : " #{lstrip}"
end

#cl_index_of_next_space(position = 0) ⇒ Object

Given a position, return the position of the next space



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/corelib_ruby/core_ext/string/queries.rb', line 15

def cl_index_of_next_space(position=0)
  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

#cl_lastObject



14
15
16
# File 'lib/corelib_ruby/core_ext/string/core.rb', line 14

def cl_last
  empty? ? "" : self[-1, 1]
end

#cl_last_or_nilObject



18
19
20
# File 'lib/corelib_ruby/core_ext/string/core.rb', line 18

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

#cl_not_empty?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/corelib_ruby/core_ext/string/core.rb', line 2

def cl_not_empty?
  !self.empty?
end

#cl_to_bool(options = {}) ⇒ Object

Convert a string to a boolean. 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



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/corelib_ruby/core_ext/string/conversions.rb', line 17

def cl_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

#cl_to_yes_no(options = {}) ⇒ Object



2
3
4
# File 'lib/corelib_ruby/core_ext/string/conversions.rb', line 2

def cl_to_yes_no(options={})
  self.cl_to_bool(options).cl_to_yes_no(options)
end