String Foundation is a Ruby library that provides useful methods for the Ruby string class.
- Table of Contents
- Quick Start
- The Blank Methods
- The Is Methods
- The Length Methods
- The Convertible Methods
- The With Methods
- The Convert Methods
- The Like Methods
- The Case Methods
- Contributing to String Foundation
- License
- Ruby version 2.1.0 or higher
- A project that uses source control, such as Git
String Foundation is available as a gem, to install it just install the gem:
$ gem install string_foundationIf you are using Bundler, add this line to your application's Gemfile:
gem "string_foundation"And then run bundle install .
The following is a sample of what String Foundation provides.
# Check characters length.
"abc".length?(3) #=> true
"password".length?(8..32) #=> true
"password".gte?(8) #=> true
"password".lte?(32) #=> true
# Compare a symbol.
"abc".is_sym?(:abc) #=> true
# Check for convertible.
"123".to_i? #=> true
"x123".to_i? #=> false
# Remove leading zeros.
"00000123".without_leading_zeros #=> "123"
# Convert a value to the appropriate type.
"false".to_pretty #=> false
".5".to_pretty #=> 0.5
# Convert to lowerCamelCase.
"user_id".to_lcamel #=> "userId"The Blank Methods enable you to check whether or not a string is empty. These
methods return true or false .
The blank? method checks whether a string is blank (it is an empty string or
includes only half-width spaces or newlines). If a string is blank, return true ,
unless return false .
"".blank? #=> true
" ".blank? #=> true
" ".blank? #=> true
" \n ".blank? #=> true
"abc".blank? #=> falseThe present? method checks whether a string is present (it is not an empty string
or does not include only half-width spaces or newlines). If a string is present,
return true , unless return false .
"".present? #=> false
" ".present? #=> false
" ".present? #=> false
" \n ".present? #=> false
"abc".present? #=> trueThe Is Methods enable you to check whether or not a string is nearly equal to
a specific value. These methods return true or false .
The is_sym? method checks whether a string is nearly equal to a specific Symbol.
This returns true only if it is, unless return false . This method accepts
only Symbol.
"abc".is_sym?(:abc) #=> true
"".is_sym?(:"") #=> true
"abc".is_sym?(:def) #=> false
"abc".is_sym?("abc") #=> ArgumentErrorThe Length methods enable you to check whether or not characters length is a
specific length. These methods return true or false .
The length? method checks whether characters length is equal to a specific
length. If its case, return true , unless return false .
This method accepts number of Integer (including Fixnum and Bignum classes).
"abc".length?(3) #=> true
"abc".length?(4) #=> false
"abc".length?("3") #=> ArgumentErrorAlso this method accepts Range. In that case, characters length is within a range,
return true , unless return false .
"abc".length?(2..5) #=> true
"abc".length?(8..32) #=> falseThe length_lt? method checks whether characters length is less than a specific
length. If its case, return true , unless return false .
This method accepts only number of Integer (including Fixnum and Bignum classes).
"abc".length_lt?(2) #=> false
"abc".length_lt?(3) #=> false
"abc".length_lt?(4) #=> trueThe length_lte? method checks whether characters length is less than or equal
to a specific length. If its case, return true , unless return false .
This method accepts only number of Integer (including Fixnum and Bignum classes).
"abc".length_lte?(2) #=> false
"abc".length_lte?(3) #=> true
"abc".length_lte?(4) #=> trueThe length_gt? method checks whether characters length is greater than a specific
length. If its case, return true , unless return false .
This method accepts only number of Integer (including Fixnum and Bignum classes).
"abc".length_gt?(2) #=> true
"abc".length_gt?(3) #=> false
"abc".length_gt?(4) #=> falseThe length_gte? method checks whether characters length is greater than or equal
to a specific length. If its case, return true , unless return false .
This method accepts only number of Integer (including Fixnum and Bignum classes).
"abc".length_gte?(2) #=> true
"abc".length_gte?(3) #=> true
"abc".length_gte?(4) #=> falseThe Convertible Methods enable you to check whether or not a string can be
converted to another class object. These methods return true or false .
The to_i? method checks whether a string can be converted to an Integer
(including Fixnum and Bignum classes). This returns true only if an argument
is convertible, therefore it is not required for the argument to be an integral
number. If you pass a floating point number as an argument to this method, it will
return true because it can be converted using the to_i Ruby built-in method
(for example, "0.4".to_i returns 0 ).
"123".to_i? #=> true
"0.3".to_i? #=> true
".2".to_i? #=> true
"abc".to_i? #=> false
"2x".to_i? #=> falseIf an argument has leading zeros, they will be removed before checking.
"00000123".to_i? #=> trueThe to_f? method is to check convertibility to the Float class. This returns
true only when an argument is convertible, therefore it is not required for the
argument to be a floating point number. If you pass an integral number as an
argument to this method, it will return true because it can be converted using
the to_f Ruby built-in method (for example, "2".to_f returns 2.0 ).
"0.3".to_f? #=> true
"2".to_f? #=> true
".2".to_f? #=> true
"abc".to_f? #=> false
"2.0x".to_f? #=> falseThe to_bool? method checks whether a string is convertible to TrueClass or
FalseClass. This returns true or false only when the string is "true" or
"false" .
"true".to_bool? #=> true
"false".to_bool? #=> true
"abc".to_bool? #=> false
"123".to_bool? #=> falseString Foundation also provides a check for a string's convertibility to a "Booly"
(truthy or falsy). This returns true only when the string is a positive number,
"true" , or an empty string. If not, it returns false .
"true".to_booly? #=> true
"123".to_booly? #=> true
"".to_booly? #=> true
"-3".to_booly? #=> true
"abc".to_booly? #=> falseThe With Methods provide you with ways to append or remove specific characters from a String object.
The without_leading_zeros method removes leading zeros (called "zero padding").
This supports a floating point number and a string starting with a plus or minus sign.
"00001".without_leading_zeros #=> "1"
"-0000.3".without_leading_zeros #=> "-0.3"
["00001", "00003", "00008"].map { |num| num.without_leading_zeros } #=> ["1", "3", "8"]The Convert Methods enable you to convert to a specific class object. While Ruby built-in methods include conversion from a String object to an Integer object or a Float object, they do not include conversion to a TrueClass / FalseClass object or a proper class object. The Convert Methods make it easy to convert to these excluded classes.
The to_bool method is for converting from a "true" or "false" string to true
or false , otherwise it will raise a TypeError .
"true".to_bool #=> true
"false".to_bool #=> false
"1".to_bool #=> TypeError
"0".to_bool #=> TypeError
"-1".to_bool #=> TypeError
"abc".to_bool #=> TypeError
"".to_bool #=> TypeErrorIf you want to convert a Booly string, you can use the to_booly method. When a
string is "true" or a positive number, this method will return true . Otherwise
it will return false .
"true".to_booly #=> true
"false".to_booly #=> false
"1".to_booly #=> true
"0".to_booly #=> false
"-1".to_booly #=> false
"abc".to_booly #=> TypeError
"".to_booly #=> falseThe to_pretty method is powerful. This method can convert to a proper class,
for example, it will return a TrueClass true when a string is "true", or returns
an Integer 1 when a string is "1".
Also this returns nil when a string is an empty string.
"1".to_pretty #=> 1
"-3".to_pretty #=> -3
"0004".to_pretty #=> 4
"0.1".to_pretty #=> 0.1
"-.5".to_pretty #=> -0.5
"00.01".to_pretty #=> 0.01
"true".to_pretty #=> true
"false".to_pretty #=> false
"".to_pretty #=> nilThe nl_to method converts a string with newlines to its specific characters.
The nl2 method is an alias for nl_to .
"Hi!\nI am Jaga Apple.".nl_to(" / ") #=> "Hi! / I am Jaga Apple."
"Hi!\nI am Jaga Apple.".nl2(" / ") #=> "Hi! / I am Jaga Apple."The nl_to_br method replaces the newlines in a string with HTML tag <br> for
break line.
The nl2br method is an alias for nl_to_br .
"Hi!\nI am Jaga Apple.".nl_to_br #=> "Hi!<br>I am Jaga Apple."
"Hi!\nI am Jaga Apple.".nl2br #=> "Hi!<br>I am Jaga Apple."The Like Methods provide to check whether a string is an integral number or a
floating point number. These method ignore leading zeros, so the string "000123"
is regarded as an integral number. These method return true or false .
The Like Methods check whether a string is an integral number or a floating point
number. These methods ignore leading zeros, so the string 000123 is regarded
as an integral number. These methods return true or false .
The like_i? method checks whether a string is an integral number.
"123".like_i? #=> true
"00123".like_i? #=> true
"0.3".like_i? #=> false
".2".like_i? #=> false
"abc".like_i? #=> false
"2x".like_i? #=> falseThe like_f? method checks whether a string is a floating point number.
"123".like_f? #=> false
"00123".like_f? #=> false
"0.3".like_f? #=> true
".2".like_f? #=> true
"abc".like_f? #=> false
"2x".like_f? #=> falseThe Case Methods convert to a case style, such as lowerCamelCase and
lower_snake_case . The following methods are available.
| METHOD NAME | CASE STYLE | EXAMPLE |
|---|---|---|
to_lcamel |
Lower Camel Case | iAmJagaApple |
to_ucamel |
Upper Camel Case | IAmJagaApple |
to_lsnake |
Lower Snake Case | i_am_jaga_apple |
to_usnake |
Upper Snake Case | I_Am_Jaga_Apple |
to_lkebab |
Lower Kebab Case (Chain Case) | i-am-jaga-apple |
to_ukebab |
Upper Kebab Case (Train Case) | I-Am-Jaga-Apple |
to_lspace |
Lower Space Case (Lower Case) | i am jaga apple |
to_uspace |
Upper Space Case (Start Case) | I Am Jaga Apple |
to_ldot |
Lower Dot Case | i.am.jaga.apple |
to_udot |
Upper Dot Case | I.Am.Jaga.Apple |
"user_id".to_lcamel #=> "userId"
"createdAt".to_lsnake #=> "created_at"Bug reports and pull requests are welcome on GitHub at https://github.com/jagaapple/string_foundation.rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
We adhere GitHub Flow to develop this project. Anything in the master branch
is deployable. To work on something new, create a descriptively named branch
off of master, also add a prefix feature/ to its name.
For more details, see GitHub Flow – Scott Chacon.
The gem is available as open source under the terms of the MIT License.
Copyright 2017 Jaga Apple. All rights reserved.
