tags:

views:

100

answers:

2

This is fairly trivial, but it bothers me to no end that I haven't yet found the answer using Google or this forum. But how can i turn this into one line? Without have to declare rooms an array above?

rooms = []
hwdata.availability.each {|room| rooms << room.name}
+7  A: 
rooms = hwdata.availability.collect {|room| room.name}

Or in Ruby 1.9, even more concise:

rooms = hwdata.availability.collect &:name
Chuck
this is perfect!
holden
A: 

Or you can use #map also.

rooms = hwdata.availability.map {|room| room.name}

khelll
Further info: `map` is just another name for `collect`. So you really can use either.
Chuck