Remove nil and blank string array in rails Array and display them concatenated

I wanted to output the results of various methods as a single string, but there were two types of method return values: the case of nil and the case of"". Only nil can be omitted from # compact of Rails Array. Omitted using reject.

pry(main)> ["Method A", nil, "", "Method B", "Method C"].reject { |e| e.to_s.empty? }.join('、')
=> "Method A, Method B, Method C"

Recommended Posts

Remove nil and blank string array in rails Array and display them concatenated
Remove "assets" and "turbolinks" in "Rails6".
Implementation policy to dynamically save and display Timezone in Rails
Display Flash messages in Rails
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
CRUD features and MVC in Rails