Stringtrain
Stringtrain is a handy internal Ruby DSL for creating dynamically pieced-together strings.
Installation
Add this line to your application's Gemfile:
gem 'stringtrain'
And then execute:
$ bundle
Or install it yourself as:
$ gem install stringtrain
Usage
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.
Global options
: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 totrue
):prefix
: A string that appears before the part. (Defaults tonil
):suffix
: A string that appears after the part. (Defaults tonil
):surround_with
: A string or array that will be used aroundstring
. If it is an array, the first and last elements will be used for the left and right side ofstring
. (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!"
Contributing
- 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