I just started to learn Erlang, and really like 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 evaluate to "london" and "boston".
What's 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 got 'til now is:
weather.collect {|w| w[:city] if w[:weather] == :fog }.compact But in this case, I have to call compact to remove nil values, and the example itself is not that readable as Erlang.
And even more, in the Erlang example, both city and weather are atoms. I don't even know how to get something that makes sense and looks good like this in Ruby.