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

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

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

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,

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

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

Frederick Cheung wrote in post #970761:

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,

Michael Pavling wrote in post #970778: