-2

I'm looking at the default file placed in /config/initializers/assets.rb in a Rails 5 app.

To precompile code, Rails inside the comments, gives a pre-existing code to precompile assets together.

Rails.application.config.assets.precompile += %w( search.js )

I am curious what the last part means: += %w( search.js ), as I have never seen the expression (+= %w) used in Ruby or Rails. I know that the entire line is calling the Rails class, chaining some methods together to create the end output. But I am curious, what does the += %w do, and how is it affecting what I presume to be the argument (search.js)?

the12
  • 2,395
  • 5
  • 19
  • 37

1 Answers1

3
Rails.application.config.assets.precompile += %w( search.js )

is equal to doing

Rails.application.config.assets.precompile = Rails.application.config.assets.precompile + ['search.js']

To break it down, += is just the regular operator for including the lefthand side in the statement. Like

i = 0
i += 1 # i = i + 1

%w enables you to create an array like this

%w(search.js profile.js error.js)

which is just a nice shorthand for

['search.js', 'profile.js', 'error.js']
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54