@public_documents = @documents.map { |document| document.to_hash }
Refactorings
No refactoring yet !
Adam
September 8, 2009, September 08, 2009 16:26, permalink
There is, in fact, something in ActiveSupport.
@documents.map(&:to_hash)
Tj Holowaychuk
September 8, 2009, September 08, 2009 16:40, permalink
&:foo calls #to_proc on an object, as Adam shows. so &:to_hash becomes:
{ |obj| obj.to_hash }
Tj Holowaychuk
September 8, 2009, September 08, 2009 16:41, permalink
Note that you can use & as an unary 'method' for any object, however it seems that most people just use it with symbols.
Is there a shorter version, or do I always have to code the block syntax? Something like @documents.collect_call(:to_hash)? Btw. It's Rails so maybe there might be something in ActiveSupport.