Stringtrain is a handy internal Ruby DSL for creating dynamically pieced-together strings.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install stringtrain
To use Stringtrain, create a new instance of the
Stringtrain::Cab class, and pass a block to it. Or you can instantize it into a variable, and use the
#part method separately.
:part_separator: A string that will serve as the delimiter between parts. Defaults to a whitespace character. (Defaults to
Individual part options
:condition: An expression that returns true or false (or just an object). If the expression returns false, then the part does not get added. (Defaults to
:prefix: A string that appears before the part. (Defaults to
:suffix: A string that appears after the part. (Defaults to
:surround_with: A string or array that will be used around
string. If it is an array, the first and last elements will be used for the left and right side of
string. (Defaults to
A simple example
See the documentation for more examples.
Stringtrain::Cab.new do part 'A monkey loves to eat', suffix: '...' part 'bananas', suffix: '!' end #=> "A monkey loves to eat... bananas!"
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Added some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request