简体   繁体   中英

one line ruby array creation and population

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM