четверг, 5 сентября 2013 г.

Ruby array to sentence string

It's very simple:
irb> ["one","two","three"].to_sentence 
=> "one, two and three"
you can use different options to humanize output:
irb> ["one","two","three"].to_sentence(words_connector: " not equal ")
=> "one not equal two and three"
irb> ["to be", "not to be"].to_sentence(two_words_connector: " or ")
=> "to be or not to be"
But remember about your default locale in Rails!
# application.rb 
config.i18n.default_locale = :ru

irb> ["one","two","three"].to_sentence
=> "one, two и three"
To read:
Array#to_sentence (apidoc)

Комментариев нет:

Отправить комментарий