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}
rooms = hwdata.availability.collect {|room| room.name}
或者在Ruby 1.9中更加简洁:
rooms = hwdata.availability.collect &:name
Or you can use #map also.
rooms = hwdata.availability.map {|room| room.name}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.