I just started learning Erlang and really love their list comprehension syntax, for example:
Weather = [{toronto, rain}, {montreal, storms}, {london, fog}, {paris, sun}, {boston, fog}, {vancounver, snow}]. FoggyPlaces = [X || {X, fog} <- Weather].
In this case, FoggyPlaces will be rated as "London" and "Boston."
What is the best way to do this in Ruby?
For example, an array like (very common, I believe):
weather = [{city: 'toronto', weather: :rain}, {city: 'montreal', weather: :storms}, {city: 'london', weather: :fog}, {city: 'paris', weather: :sun}, {city: 'boston', weather: :fog}, {city: 'vancounver', weather: :snow}]
The best I've got so far:
weather.collect {|w| w[:city] if w[:weather] == :fog }.compact
But in this case, I need to call compact to remove the nil values, and the example itself does not read like Erlang.
And even more, in the example of Erlang, both city and weather are atoms. I donβt even know how to get something that makes sense and looks like in Ruby.