The gem introduces the Arrayish::String class, a string that has some array characteristics.
Arrayish::String simplifies and DRYs code where an array appears in a string with delimiters.
Where what is effectively array data is supplied as a string with separators, this can simplify code. Example
> fruits = Arrayish::String.new('apples,pears,oranges')
> fruits[1..-1]
=> "pears,oranges"
Contrast the same thing with an ordinary string
> fruits = 'apples,pears,oranges'
> fruits.split(',')[1..-1].join(',')
=> "pears,oranges"
The gem was written after enountering code that with littered with lines like the above, with repeated splits and joins. The meaning of the code was obfuscated by the chained transformations.
An Arrayish::String can be cast to an array or string as required.
> fruits.to_s[0..4]
=> "apple"
> fruits.to_a
=> ["apples", "pears", "oranges"]
Add this line to your application's Gemfile:
gem 'arrayish'
And then execute:
$ bundle
Or install it yourself as:
$ gem install arrayish
The separator will be a comma.
> fruits = Arrayish::String.new('apples,pears,oranges')
> require 'arrayish'
> class FruitList < Arrayish::String
> def separator
> '|'
> end
> end
> fruits = FruitList.new('apples|pears')
=> "apples|pears"
> fruits[0]
=> "apples"
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request