Class: String
- 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
-
#cl_all_digits?(if_empty: false) ⇒ Boolean
Returns true if a non empty string contains all digit characters.
-
#cl_all_letters?(if_empty: false) ⇒ Boolean
Returns true if a non empty string contains all letters.
-
#cl_all_spaces?(when_empty: false) ⇒ Boolean
Returns true if a non empty string contains all space characters.
-
#cl_combine(*args) ⇒ Object
Combines two strings together with a little more intelligence, using a separator.
-
#cl_concat_with(str, separator = "") ⇒ Object
Does the same thing as String#contact, but allows a separator to be inserted between the two strings.
-
#cl_excerpt_to_end_of_word(position = nil) ⇒ Object
Returns the subset of a string from [0, position] if string is a space.
- #cl_first ⇒ Object
- #cl_first_or_nil ⇒ Object
-
#cl_first_words(n = nil, ellipses: false) ⇒ Object
Retreive the fist n words of a string as a new string.
-
#cl_force_leading_space(even_when_empty: false) ⇒ Object
Forces a string to have at least one leading space.
-
#cl_index_of_next_space(position = 0) ⇒ Object
Given a position, return the position of the next space.
- #cl_last ⇒ Object
- #cl_last_or_nil ⇒ Object
- #cl_not_empty? ⇒ Boolean
-
#cl_to_bool(options = {}) ⇒ Object
Convert a string to a boolean.
- #cl_to_yes_no(options = {}) ⇒ Object
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.
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.
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.
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)
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) = args. raise ArgumentError, "You need to supply at least one string" if args.empty? str = self args.each { |val| str = str.cl_private_combine(val, ) } return .fetch(:if_empty, "") if str.nil? || str.empty? prefix = .fetch(:prefix, nil) str = "#{prefix}#{str}" if .fetch(:wrap, true) && (prefix.cl_not_nil?) suffix = .fetch(:suffix, nil) str = "#{str}#{suffix}" if .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
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_first ⇒ Object
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_nil ⇒ Object
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_last ⇒ Object
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_nil ⇒ Object
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
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(={}) strip = .fetch(:strip, true) strict = .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(={}) self.cl_to_bool().cl_to_yes_no() end |