Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/string_foundation/is.rb,
lib/string_foundation/case.rb,
lib/string_foundation/like.rb,
lib/string_foundation/with.rb,
lib/string_foundation/blank.rb,
lib/string_foundation/length.rb,
lib/string_foundation/convert.rb,
lib/string_foundation/convertible.rb
Overview
LIB - STRING FOUNDATION - LENGTH frozen_string_literal: true
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Check whether a string is empty or only half-width spaces.
- #is_sym?(symbol) ⇒ Boolean
-
#length?(length) ⇒ Boolean
Check whether characters length is a specific number.
-
#length_gt?(length) ⇒ Boolean
Check whether characters length is greater than a specific number.
-
#length_gte?(length) ⇒ Boolean
Check whether characters length is greater than or equal to a specific number.
-
#length_lt?(length) ⇒ Boolean
Check whether characters length is less than a specific number.
-
#length_lte?(length) ⇒ Boolean
Check whether characters length is less than or equal to a specific number.
-
#like_f? ⇒ Boolean
Check whether a string is a floating point number.
-
#like_i? ⇒ Boolean
Check whether a string is an integral number.
-
#nl_to(char) ⇒ Object
(also: #nl2)
Convert from newline character to specific characters.
-
#nl_to_br ⇒ Object
(also: #nl2br)
Convert from newline character to a HTML tag “
”. -
#present? ⇒ Boolean
Check whether a string is not empty or only half-width spaces.
-
#to_bool ⇒ Object
Convert to TrueClass or FalseClass.
-
#to_bool? ⇒ Boolean
Whether or not to be possible to covert String to Boolean.
-
#to_booly ⇒ Object
Convert a booly string to TrueClass or FalseClass.
-
#to_booly? ⇒ Boolean
Whether or not to be possible to covert String to something which behaves like boolean types.
-
#to_f? ⇒ Boolean
Whether or not to be possible to covert String to Float.
-
#to_i? ⇒ Boolean
Whether or not to be possible to covert String to Integer.
-
#to_lcamel ⇒ Object
Convert to lowerCamelCase.
-
#to_ldot ⇒ Object
Convert to lower.dot.case.
-
#to_lkebab ⇒ Object
Convert to lower-kebab-case.
-
#to_lsnake ⇒ Object
Convert to lower_snake_case.
-
#to_lspace ⇒ Object
Convert to lower space case.
-
#to_pretty ⇒ Object
Convert to a pretty value.
-
#to_ucamel ⇒ Object
Convert to UpperCamelCase.
-
#to_udot ⇒ Object
Convert to Upper.Dot.Case.
-
#to_ukebab ⇒ Object
Convert to Upper-Kebab-Case.
-
#to_usnake ⇒ Object
Convert to Upper_Snake_Case.
-
#to_uspace ⇒ Object
Convert to Upper Space Case.
-
#without_leading_zeros ⇒ Object
(also: #without_zero_pad)
Remove leading zeros.
Instance Method Details
#blank? ⇒ Boolean
Check whether a string is empty or only half-width spaces.
8 9 10 |
# File 'lib/string_foundation/blank.rb', line 8 def blank? self.empty? || /\A[[:space:]]*\z/.match(self).instance_of?(MatchData) end |
#is_sym?(symbol) ⇒ Boolean
7 8 9 10 11 |
# File 'lib/string_foundation/is.rb', line 7 def is_sym?(symbol) raise ArgumentError.new("argument must be Symbol") unless symbol.instance_of?(Symbol) (self == symbol.to_s) end |
#length?(length) ⇒ Boolean
Check whether characters length is a specific number.
8 9 10 11 12 13 14 15 |
# File 'lib/string_foundation/length.rb', line 8 def length?(length) unless [Integer, Range, Fixnum, Bignum].include?(length.class) raise ArgumentError.new("argument must be Integer (including Fixnum or Bignum) or Range") end return (self.length == length) unless length.instance_of?(Range) self.length.between?(length.first, length.last) end |
#length_gt?(length) ⇒ Boolean
Check whether characters length is greater than a specific number.
32 33 34 35 36 |
# File 'lib/string_foundation/length.rb', line 32 def length_gt?(length) self.instance_eval { allow_only_integer(length) } (self.length > length) end |
#length_gte?(length) ⇒ Boolean
Check whether characters length is greater than or equal to a specific number.
39 40 41 42 43 |
# File 'lib/string_foundation/length.rb', line 39 def length_gte?(length) self.instance_eval { allow_only_integer(length) } (self.length >= length) end |
#length_lt?(length) ⇒ Boolean
Check whether characters length is less than a specific number.
18 19 20 21 22 |
# File 'lib/string_foundation/length.rb', line 18 def length_lt?(length) self.instance_eval { allow_only_integer(length) } (self.length < length) end |
#length_lte?(length) ⇒ Boolean
Check whether characters length is less than or equal to a specific number.
25 26 27 28 29 |
# File 'lib/string_foundation/length.rb', line 25 def length_lte?(length) self.instance_eval { allow_only_integer(length) } (self.length <= length) end |
#like_f? ⇒ Boolean
Check whether a string is a floating point number.
17 18 19 20 21 22 |
# File 'lib/string_foundation/like.rb', line 17 def like_f? return false unless self.to_f? num = self.without_leading_zeros (num.to_i != num.to_f) || num.include?(".") end |
#like_i? ⇒ Boolean
Check whether a string is an integral number.
9 10 11 12 13 14 |
# File 'lib/string_foundation/like.rb', line 9 def like_i? return false unless self.to_i? num = self.without_leading_zeros (num.to_i == num.to_i) && !num.include?(".") end |
#nl_to(char) ⇒ Object Also known as: nl2
Convert from newline character to specific characters.
39 40 41 42 |
# File 'lib/string_foundation/convert.rb', line 39 def nl_to(char) char = "" if char.nil? self.gsub(/(\r\n|\n)/, char) end |
#nl_to_br ⇒ Object Also known as: nl2br
Convert from newline character to a HTML tag “
”.
45 46 47 |
# File 'lib/string_foundation/convert.rb', line 45 def nl_to_br self.nl_to("<br>") end |
#present? ⇒ Boolean
Check whether a string is not empty or only half-width spaces.
13 14 15 |
# File 'lib/string_foundation/blank.rb', line 13 def present? !(self.blank?) end |
#to_bool ⇒ Object
Convert to TrueClass or FalseClass.
11 12 13 14 15 16 17 |
# File 'lib/string_foundation/convert.rb', line 11 def to_bool unless self.to_bool? raise TypeError.new("#{self} cannot be converted to TrueClass or FalseClass") end (self == "true") end |
#to_bool? ⇒ Boolean
Whether or not to be possible to covert String to Boolean.
25 26 27 28 |
# File 'lib/string_foundation/convertible.rb', line 25 def to_bool? return true if ["true", "false"].include?(self) false end |
#to_booly ⇒ Object
Convert a booly string to TrueClass or FalseClass.
20 21 22 23 24 25 26 27 |
# File 'lib/string_foundation/convert.rb', line 20 def to_booly unless self.to_booly? raise TypeError.new("#{self} cannot be converted to TrueClass or FalseClass") end return true if self == "true" || (self.to_f? && self.to_f > 0) false end |
#to_booly? ⇒ Boolean
Whether or not to be possible to covert String to something which behaves like boolean types.
32 33 34 35 36 37 38 |
# File 'lib/string_foundation/convertible.rb', line 32 def to_booly? return true if self.length == 0 return true if ["true", "false"].include?(self) return true if self.to_f? false end |
#to_f? ⇒ Boolean
Whether or not to be possible to covert String to Float.
17 18 19 20 21 22 |
# File 'lib/string_foundation/convertible.rb', line 17 def to_f? Float(self.without_leading_zeros) true rescue ArgumentError false end |
#to_i? ⇒ Boolean
Whether or not to be possible to covert String to Integer.
9 10 11 12 13 14 |
# File 'lib/string_foundation/convertible.rb', line 9 def to_i? Integer(Float(self.without_leading_zeros)) true rescue ArgumentError false end |
#to_lcamel ⇒ Object
Convert to lowerCamelCase.
8 9 10 11 |
# File 'lib/string_foundation/case.rb', line 8 def to_lcamel ucamel = self.to_ucamel ucamel.instance_eval { make_head_lower } end |
#to_ldot ⇒ Object
Convert to lower.dot.case.
64 65 66 67 |
# File 'lib/string_foundation/case.rb', line 64 def to_ldot udot = self.to_udot udot.downcase end |
#to_lkebab ⇒ Object
Convert to lower-kebab-case.
36 37 38 39 |
# File 'lib/string_foundation/case.rb', line 36 def to_lkebab ukebab = self.to_ukebab ukebab.downcase end |
#to_lsnake ⇒ Object
Convert to lower_snake_case.
22 23 24 25 |
# File 'lib/string_foundation/case.rb', line 22 def to_lsnake usnake = self.to_usnake usnake.downcase end |
#to_lspace ⇒ Object
Convert to lower space case.
50 51 52 53 |
# File 'lib/string_foundation/case.rb', line 50 def to_lspace uspace = self.to_uspace uspace.downcase end |
#to_pretty ⇒ Object
Convert to a pretty value.
30 31 32 33 34 35 36 |
# File 'lib/string_foundation/convert.rb', line 30 def to_pretty return self.without_leading_zeros.to_i if self.like_i? return self.without_leading_zeros.to_f if self.like_f? return self.to_bool if self.to_bool? (self.length > 0) ? self : nil end |
#to_ucamel ⇒ Object
Convert to UpperCamelCase.
14 15 16 17 18 19 |
# File 'lib/string_foundation/case.rb', line 14 def to_ucamel split_camel.map do |cw| cw.split(/\.|_|-|\s/).map { |w| w.capitalize }.join end .join end |
#to_udot ⇒ Object
Convert to Upper.Dot.Case.
70 71 72 73 74 75 |
# File 'lib/string_foundation/case.rb', line 70 def to_udot split_camel.map do |cw| cw.split(/\.|_|-|\s/).map { |w| w.capitalize }.join(".") end .join(".") end |
#to_ukebab ⇒ Object
Convert to Upper-Kebab-Case.
42 43 44 45 46 47 |
# File 'lib/string_foundation/case.rb', line 42 def to_ukebab split_camel.map do |cw| cw.split(/\.|_|-|\s/).map { |w| w.capitalize }.join("-") end .join("-") end |
#to_usnake ⇒ Object
Convert to Upper_Snake_Case.
28 29 30 31 32 33 |
# File 'lib/string_foundation/case.rb', line 28 def to_usnake split_camel.map do |cw| cw.split(/\.|_|-|\s/).map { |w| w.capitalize }.join("_") end .join("_") end |
#to_uspace ⇒ Object
Convert to Upper Space Case.
56 57 58 59 60 61 |
# File 'lib/string_foundation/case.rb', line 56 def to_uspace split_camel.map do |cw| cw.split(/\.|_|-|\s/).map { |w| w.capitalize }.join(" ") end .join(" ") end |
#without_leading_zeros ⇒ Object Also known as: without_zero_pad
Remove leading zeros.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/string_foundation/with.rb', line 8 def without_leading_zeros return self if self == "0" is_positive = self.start_with?("0") is_negative = self.start_with?("-0") if is_positive || is_negative sig = self[0, self.length].gsub(/(^0+)|(^-0+)/, "") sig = "0" + sig if sig.start_with?(".") || sig.length == 0 sig = "-" + sig if is_negative && sig != "0" sig else self end end |