I sit possible to write it in one line Array init + Array loading

I am always writing it in 2 lines : ( Array init then loading the
array )

@providers = []
@authentications.map {|authentification| @providers <<

authentification.provider }

Is there any Ruby writing for writing just one line ?

thanks fyh

On 25 December 2010 15:23, Erwin [email protected] wrote:

I am always writing it in 2 lines : ( Array init then loading the
array )

@providers = []
@authentications.map {|authentification| @providers <<
authentification.provider }

Is there any Ruby writing for writing just one line ?

@providers = @authentications.map { |authentication|
authentication.provider }

With ActiveSupport from Rails it’s also possible:
@providers = @authentications.map(&:provider)

Thanks, Ivan Povalyukhin

ivanpoval wrote in post #970734:

With ActiveSupport from Rails it’s also possible:
@providers = @authentications.map(&:provider)

However, this is slightly slower, so while I use it in tests, I tend not
to use it in application code.


Thanks, Ivan Povalyukhin

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

On Dec 26, 9:22am, ivanpoval [email protected] wrote:

With ActiveSupport from Rails it’s also possible:
@providers = @authentications.map(&:provider)

As of ruby 1.8.7 it’s built into ruby (no active support required)

Fred

On 26 December 2010 14:30, Marnen Laibow-Koser [email protected]
wrote:

ivanpoval wrote in post #970734:

With ActiveSupport from Rails it’s also possible:
@providers = @authentications.map(&:provider)

However, this is slightly slower, so while I use it in tests, I tend not
to use it in application code.

But it’s much faster to type, and easy to read… you’re not
prematurely optimising, are you? :wink:

Frederick C. wrote in post #970761:

On Dec 26, 9:22am, ivanpoval [email protected] wrote:

With ActiveSupport from Rails it’s also possible:
@providers = @authentications.map(&:provider)

As of ruby 1.8.7 it’s built into ruby (no active support required)

1.8.7? I thought it didn’t make it in until later. Good to know.

Fred

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

Michael P. wrote in post #970778:

On 26 December 2010 14:30, Marnen Laibow-Koser [email protected]
wrote:

ivanpoval wrote in post #970734:

With ActiveSupport from Rails it’s also possible:
@providers = @authentications.map(&:provider)

However, this is slightly slower, so while I use it in tests, I tend not
to use it in application code.

But it’s much faster to type, and easy to read… you’re not
prematurely optimising, are you? :wink:

When a premature optimization is this simple, and this free of
disadvantages, there’s no reason not do to it. Any more complex and I
wouldn’t. :slight_smile:

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone