I have the following array
cities = [\"Kathmandu\", \"Pokhara\", \"\", \"Dharan\", \"Butwal\"]
I want to remove blank elements from the ar
cities.reject! { |c| c.blank? }
The reason you want to use blank? over empty? is that blank recognizes nil, empty strings, and white space. For example:
cities = ["Kathmandu", "Pokhara", " ", nil, "", "Dharan", "Butwal"].reject { |c| c.blank? }
would still return:
["Kathmandu", "Pokhara", "Dharan", "Butwal"]
And calling empty? on " " will return false, which you probably want to be true.
Note: blank? is only accessible through Rails, Ruby only supports empty?.