Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

ActiveSupport | Array#to_sentence

概要

Array#to_sentence

詳細

Array#to_sentence について

Array#to_sentence

リストを英文で単語を列挙する際のフォーマットにする。
詳細はサンプル参照。

サンプル

# encoding: utf-8
require 'active_support/core_ext/array/conversions'
require 'tbpgr_utils'

I18n.enforce_available_locales = true
bulk_puts_eval binding, <<-EOS
%w!hoge!.to_sentence
%w!hoge hige!.to_sentence
%w!hoge hige hage!.to_sentence
EOS

__END__
・下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

%w!hoge!.to_sentence # => "hoge"
%w!hoge hige!.to_sentence # => "hoge and hige"
%w!hoge hige hage!.to_sentence # => "hoge, hige, and hage"