Episodes
Luke Stutters said about 1 year ago on Gem in a Box :
Thanks Dave, very topical!

But seriously, why [].tap?

Sathishkumar Natesan said about 1 year ago on Gem in a Box :
Thanks Dave. As informative as ever.

A trivial typo in config.ru -

hash[:password] == password

David Kimura PRO said about 1 year ago on Gem in a Box :
  Luke Stutters It's just a preference when creating an array or hash. In this particular case there wouldn't be any benefit over
array = []
array << { username: 'username', password: 'password' }

However, in many other situations doing a [].tap or {}.tap is pretty handy in conditionally adding items. For example,

User.new(user_params)

def user_params
  allowed_params = [].tap do |array|
    array << :first_name
    array << :last_name
    array << :admin if current_user.admin?
  end
  params.require(:user).permit(allowed_params)
end



Login to Comment