class String

Public Instance Methods

blank?() click to toggle source

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
is_sym?(symbol) click to toggle source
# 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) click to toggle source

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
length_gt?(length) click to toggle source

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
length_gte?(length) click to toggle source

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
length_lt?(length) click to toggle source

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
length_lte?(length) click to toggle source

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
like_f?() click to toggle source

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
like_i?() click to toggle source

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
nl2(char)
Alias for: nl_to
nl2br()
Alias for: nl_to_br
nl_to(char) click to toggle source

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
Also aliased as: nl2
nl_to_br() click to toggle source

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
Also aliased as: nl2br
present?() click to toggle source

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
to_bool() click to toggle source

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
to_bool?() click to toggle source

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
to_booly() click to toggle source

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
to_booly?() click to toggle source

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
to_f?() click to toggle source

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
to_i?() click to toggle source

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
to_lcamel() click to toggle source

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
to_ldot() click to toggle source

Convert to lower.dot.case.

# File lib/string_foundation/case.rb, line 64
def to_ldot
  udot = self.to_udot
  udot.downcase
end
to_lkebab() click to toggle source

Convert to lower-kebab-case.

# File lib/string_foundation/case.rb, line 36
def to_lkebab
  ukebab = self.to_ukebab
  ukebab.downcase
end
to_lsnake() click to toggle source

Convert to lower_snake_case.

# File lib/string_foundation/case.rb, line 22
def to_lsnake
  usnake = self.to_usnake
  usnake.downcase
end
to_lspace() click to toggle source

Convert to lower space case.

# File lib/string_foundation/case.rb, line 50
def to_lspace
  uspace = self.to_uspace
  uspace.downcase
end
to_pretty() click to toggle source

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
to_ucamel() click to toggle source

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
to_udot() click to toggle source

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
to_ukebab() click to toggle source

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
to_usnake() click to toggle source

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
to_uspace() click to toggle source

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
without_leading_zeros() click to toggle source

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
Also aliased as: without_zero_pad
without_zero_pad()

Private Instance Methods

allow_only_integer(argument) click to toggle source

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_head_lower() click to toggle source

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_camel() click to toggle source

Split string according to camel case.

# File lib/string_foundation/case.rb, line 83
def split_camel
  self.split(/(?=[A-Z])/)
end