class String
¶ ↑
¶ ↑
¶ ↑
¶ ↑
¶ ↑
Public Instance Methods
Check whether a string is empty or only half-width spaces.
# File lib/string_foundation/blank.rb, line 8 def blank? self.empty? || /\A[[:space:]]*\z/.match(self).instance_of?(MatchData) end
# 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
Check whether characters length is a specific number.
# 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
Check whether characters length is greater than a specific number.
# File lib/string_foundation/length.rb, line 32 def length_gt?(length) self.instance_eval { allow_only_integer(length) } (self.length > length) end
Check whether characters length is greater than or equal to a specific number.
# File lib/string_foundation/length.rb, line 39 def length_gte?(length) self.instance_eval { allow_only_integer(length) } (self.length >= length) end
Check whether characters length is less than a specific number.
# File lib/string_foundation/length.rb, line 18 def length_lt?(length) self.instance_eval { allow_only_integer(length) } (self.length < length) end
Check whether characters length is less than or equal to a specific number.
# File lib/string_foundation/length.rb, line 25 def length_lte?(length) self.instance_eval { allow_only_integer(length) } (self.length <= length) end
Check whether a string is a floating point number.
# 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
Check whether a string is an integral number.
# 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
Convert from newline character to specific characters.
# File lib/string_foundation/convert.rb, line 39 def nl_to(char) char = "" if char.nil? self.gsub(/(\r\n|\n)/, char) end
Convert from newline character to a HTML tag “
”.
# File lib/string_foundation/convert.rb, line 45 def nl_to_br self.nl_to("<br>") end
Check whether a string is not empty or only half-width spaces.
# File lib/string_foundation/blank.rb, line 13 def present? !(self.blank?) end
Convert to TrueClass or FalseClass.
# 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
Whether or not to be possible to covert String
to Boolean.
# File lib/string_foundation/convertible.rb, line 25 def to_bool? return true if ["true", "false"].include?(self) false end
Convert a booly string to TrueClass or FalseClass.
# 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
Whether or not to be possible to covert String
to something which behaves like boolean types.
# 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
Whether or not to be possible to covert String
to Float.
# File lib/string_foundation/convertible.rb, line 17 def to_f? Float(self.without_leading_zeros) true rescue ArgumentError false end
Whether or not to be possible to covert String
to Integer.
# File lib/string_foundation/convertible.rb, line 9 def to_i? Integer(Float(self.without_leading_zeros)) true rescue ArgumentError false end
Convert to lowerCamelCase.
# File lib/string_foundation/case.rb, line 8 def to_lcamel ucamel = self.to_ucamel ucamel.instance_eval { make_head_lower } end
Convert to lower.dot.case.
# File lib/string_foundation/case.rb, line 64 def to_ldot udot = self.to_udot udot.downcase end
Convert to lower-kebab-case.
# File lib/string_foundation/case.rb, line 36 def to_lkebab ukebab = self.to_ukebab ukebab.downcase end
Convert to lower_snake_case.
# File lib/string_foundation/case.rb, line 22 def to_lsnake usnake = self.to_usnake usnake.downcase end
Convert to lower space case.
# File lib/string_foundation/case.rb, line 50 def to_lspace uspace = self.to_uspace uspace.downcase end
Convert to a pretty value.
# 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
Convert to UpperCamelCase.
# 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
Convert to Upper.Dot.Case.
# 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
Convert to Upper-Kebab-Case.
# 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
Convert to Upper_Snake_Case.
# 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
Convert to Upper Space Case.
# 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
Remove leading zeros.
# 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
Private Instance Methods
Allow only Integer.
# File lib/string_foundation/length.rb, line 51 def allow_only_integer(argument) return if [Integer, Fixnum, Bignum].include?(argument.class) raise ArgumentError.new("argument must be Integer (including Fixnum or Bignum)") end
Make first character lower case.
# File lib/string_foundation/case.rb, line 88 def make_head_lower self[0].downcase + self[1..-1] end
Split string according to camel case.
# File lib/string_foundation/case.rb, line 83 def split_camel self.split(/(?=[A-Z])/) end