↩ More Dr Nic Utilities

Map By Method

Get Version 0.8.1

["1","2","3"]. map_by_to_i => [1,2,3]

Installation

gem install map_by_method

Usage

> a = ["1", "2", "3"]
> a.map_by_to_i
=> [1, 2, 3]
Can be used with ActiveRecord associations (since 0.7.0)
> company = Company.find_by_name "Dr Nic Academy" 
> company.employees.count
=> 1
> company.employees.map_by_name
=> ["Dr Nic"]

Why use this?

I think its much easier to type and read than its two equivalents:

> company.employees.map { |employee| employee.name }
or
> company.employees.map &:name

Related articles

A bunch of examples and releases

Dr Nic’s Blog

http://drnicwilliams.com – for future announcements and other stories and things.

Forum

Discussion about the Map by Methods is on the Dr Nic Utilities forum:

http://groups.google.com/group/drnicutilities

Licence

This code is free to use under the terms of the MIT licence.

Contact

Comments are welcome. Send an email to Dr Nic Williams via the forum

Dr Nic, 11th August 2007
Theme extended from Paul Battley