map Image that returns the id of a group with 5 or more people as an array
ids = groups.map{ |group| group.id if group.count >= 5}
#=> [1, 2, nil, 4,・ ・ ・]
Elements that do not meet the conditions will be nil
.reject(&:blank?) Eliminate nil and empty strings from arrays
ids = groups.map{ |group| group.id if group.count >= 5}.reject(&:blank?)
#=> [1, 2, 4,・ ・ ・]
filter_map Summarize the above. Available from Ruby 2.7
ids = groups.filter_map{ |group| group.id if group.count >= 5}
#=> [1, 2, 4,・ ・ ・]
join Returns a concatenated string. You can insert the character string in the argument
['A','B','C'].join
#=> "ABC"
['A','B','C'].join('|')
#=> "A|B|C"
Recommended Posts