Class: String
Instance Method Summary collapse
-
#combine(str, options = {}) ⇒ Object
TODO - Needs Tests Combines two strings together with a separator.
-
#concat_with(str, separator = "") ⇒ Object
Does the same thing as String#contact, but allows a separator to be inserted between the two strings.
-
#excerpt_to_end_of_word(position = nil) ⇒ Object
Returns the subset of a string from [0, position] if string is a space.
-
#first ⇒ Object
TODO - Needs Tests.
-
#index_of_next_space_from(position) ⇒ Object
Given a position, return the position of the next space.
-
#last ⇒ Object
TODO - Needs Tests.
- #not_empty? ⇒ Boolean
-
#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.
- #to_yes_no(options = {}) ⇒ Object
Instance Method Details
#combine(str, options = {}) ⇒ Object
TODO - Needs Tests Combines two strings together with a separator.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/corelib/string/core.rb', line 17 def combine(str, ={}) strip = .fetch(:strip, true) (return strip ? self.strip : self.dup) if str.nil? or str.empty? (return strip ? str.strip : str.dup) if self.empty? separator = .fetch(:separator, " ") if strip pre = self.strip post = str.strip else pre = self.dup post = str.dup end return pre + post if separator.empty? # TODO - Support other separators other than spaces. For instance if someone wanted to join with a comma # and pre ended with a comma, we could have an option to disallow repeating pre + separator + post end |
#concat_with(str, separator = "") ⇒ Object
Does the same thing as String#contact, but allows a separator to be inserted between the two strings.
41 42 43 44 45 46 |
# File 'lib/corelib/string/core.rb', line 41 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
77 78 79 80 81 82 83 84 |
# File 'lib/corelib/string/core.rb', line 77 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 |
#first ⇒ Object
TODO - Needs Tests
10 11 12 13 |
# File 'lib/corelib/string/core.rb', line 10 def first return nil if self.empty? self[0,1] end |
#index_of_next_space_from(position) ⇒ Object
Given a position, return the position of the next space
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/corelib/string/core.rb', line 87 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 |
#last ⇒ Object
TODO - Needs Tests
4 5 6 7 |
# File 'lib/corelib/string/core.rb', line 4 def last return nil if self.empty? self[-1,1] end |
#not_empty? ⇒ Boolean
70 71 72 |
# File 'lib/corelib/string/core.rb', line 70 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
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/corelib/string/core.rb', line 56 def 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 |
#to_yes_no(options = {}) ⇒ Object
48 49 50 |
# File 'lib/corelib/string/core.rb', line 48 def to_yes_no(={}) self.to_bool().to_yes_no() end |