4 ms·
In Ruby, with ruby underscore (http://bit.ly/9dwWAX http://bit.ly/9dwWAX), you can write it as: all_users.select(_.is_admin).map _.username Other language
by DanielRibeiro 16y ago
In Ruby, with ruby underscore (http://bit.ly/9dwWAX http://bit.ly/9dwWAX), you can write it as:
all_users.select(_.is_admin).map _.username
Other languages have similar ways to write it concisely as well (http://bit.ly/a6QmgL http://bit.ly/a6QmgL). Coffeescript (which compiles to javascript, and therefore, can run on Rhinon inside the jvm) in particular:
allowed_usernames = user.username for user in all_users when user.is_admin()
Yeah. Reminds me of http://gag.googlecode.com/svn/trunk/javadoc/com/google/gag/annotation/remark/ThisWouldBeOneLineIn.html http://gag.googlecode.com/svn/trunk/javadoc/com/google/gag/a...
- generalk 16y agoIn Ruby 1.9, or Ruby 1.8 w/ ActiveSupport, you can use the & operator, which coerces it's argument (in this case, a symbol) to a Proc: all_users.select(&:is_admin).map(&:username) This is a common idiom with a fantastic writeup at http://weblog.raganwald.com/2008/02/1100inject.html http://weblog.raganwald.com/2008/02/1100inject.html
- DanielRibeiro 16y agoActually on ruby 1.8.7 the proc coercion is default. No need for active support. And yeah, ruby underscore is overkill for this trivial example.