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