Permalink
Browse files

Reduce string objects by using \ instead of + or << for concatenating…

… strings

(I personally prefer writing one string in one line no matter how long it is, though)
1 parent d688814 commit b70fc698e157f2a768ba42efac08c08f4786b01c @amatsuda amatsuda committed Jan 12, 2017
Showing with 567 additions and 567 deletions.
  1. +2 −2 actioncable/lib/action_cable/connection/client_socket.rb
  2. +2 −2 actionmailer/lib/action_mailer/test_case.rb
  3. +5 −5 actionmailer/test/mail_helper_test.rb
  4. +1 −1 actionpack/lib/action_dispatch/middleware/cookies.rb
  5. +2 −2 actionpack/lib/action_dispatch/middleware/remote_ip.rb
  6. +2 −2 actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
  7. +2 −2 actionpack/lib/action_dispatch/testing/integration.rb
  8. +1 −1 actionpack/test/controller/filters_test.rb
  9. +1 −1 actionpack/test/dispatch/session/cookie_store_test.rb
  10. +1 −1 actionview/lib/action_view/helpers/url_helper.rb
  11. +3 −3 actionview/lib/action_view/renderer/partial_renderer.rb
  12. +1 −1 actionview/lib/action_view/template/handlers/builder.rb
  13. +1 −1 actionview/test/activerecord/form_helper_activerecord_test.rb
  14. +191 −191 actionview/test/template/form_helper/form_with_test.rb
  15. +207 −207 actionview/test/template/form_helper_test.rb
  16. +118 −118 actionview/test/template/form_options_helper_test.rb
  17. +5 −5 actionview/test/template/render_test.rb
  18. +1 −1 actionview/test/template/url_helper_test.rb
  19. +1 −1 activerecord/lib/active_record/associations/has_one_association.rb
  20. +3 −3 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
  21. +2 −2 activerecord/lib/active_record/tasks/database_tasks.rb
  22. +2 −2 activerecord/test/cases/adapters/postgresql/connection_test.rb
  23. +1 −1 activerecord/test/cases/associations/has_one_associations_test.rb
  24. +1 −1 activerecord/test/cases/connection_pool_test.rb
  25. +2 −2 activerecord/test/cases/nested_attributes_with_callbacks_test.rb
  26. +1 −1 activesupport/test/core_ext/string_ext_test.rb
  27. +1 −1 railties/lib/rails/application.rb
  28. +2 −2 railties/lib/rails/application/bootstrap.rb
  29. +2 −2 railties/lib/rails/generators/actions/create_migration.rb
  30. +2 −2 railties/lib/rails/generators/base.rb
  31. +1 −1 railties/lib/rails/generators/rails/plugin/plugin_generator.rb
@@ -90,8 +90,8 @@ def close(code = nil, reason = nil)
reason ||= ""
unless code == 1000 || (code >= 3000 && code <= 4999)
- raise ArgumentError, "Failed to execute 'close' on WebSocket: " +
- "The code must be either 1000, or between 3000 and 4999. " +
+ raise ArgumentError, "Failed to execute 'close' on WebSocket: " \
+ "The code must be either 1000, or between 3000 and 4999. " \
"#{code} is neither."
end
@@ -4,8 +4,8 @@
module ActionMailer
class NonInferrableMailerError < ::StandardError
def initialize(name)
- super "Unable to determine the mailer to test from #{name}. " +
- "You'll need to specify it using tests YourMailer in your " +
+ super "Unable to determine the mailer to test from #{name}. " \
+ "You'll need to specify it using tests YourMailer in your " \
"test case definition"
end
end
@@ -2,11 +2,11 @@
class HelperMailer < ActionMailer::Base
def use_mail_helper
- @text = "But soft! What light through yonder window breaks? It is the east, " +
- "and Juliet is the sun. Arise, fair sun, and kill the envious moon, " +
- "which is sick and pale with grief that thou, her maid, art far more " +
- "fair than she. Be not her maid, for she is envious! Her vestal " +
- "livery is but sick and green, and none but fools do wear it. Cast " +
+ @text = "But soft! What light through yonder window breaks? It is the east, " \
+ "and Juliet is the sun. Arise, fair sun, and kill the envious moon, " \
+ "which is sick and pale with grief that thou, her maid, art far more " \
+ "fair than she. Be not her maid, for she is envious! Her vestal " \
+ "livery is but sick and green, and none but fools do wear it. Cast " \
"it off!"
mail_with_defaults do |format|
@@ -572,7 +572,7 @@ def initialize(parent_jar)
super
if ActiveSupport::LegacyKeyGenerator === key_generator
- raise "You didn't set secrets.secret_key_base, which is required for this cookie jar. " +
+ raise "You didn't set secrets.secret_key_base, which is required for this cookie jar. " \
"Read the upgrade documentation to learn more about this new config option."
end
@@ -131,8 +131,8 @@ def calculate_ip
should_check_ip = @check_ip && client_ips.last && forwarded_ips.last
if should_check_ip && !forwarded_ips.include?(client_ips.last)
# We don't know which came from the proxy, and which from the user
- raise IpSpoofAttackError, "IP spoofing attack?! " +
- "HTTP_CLIENT_IP=#{@req.client_ip.inspect} " +
+ raise IpSpoofAttackError, "IP spoofing attack?! " \
+ "HTTP_CLIENT_IP=#{@req.client_ip.inspect} " \
"HTTP_X_FORWARDED_FOR=#{@req.x_forwarded_for.inspect}"
end
@@ -8,8 +8,8 @@ module ActionDispatch
module Session
class SessionRestoreError < StandardError #:nodoc:
def initialize
- super("Session contains objects whose class definition isn't available.\n" +
- "Remember to require the classes for all objects kept in the session.\n" +
+ super("Session contains objects whose class definition isn't available.\n" \
+ "Remember to require the classes for all objects kept in the session.\n" \
"(Original exception: #{$!.message} [#{$!.class}])\n")
set_backtrace $!.backtrace
end
@@ -145,8 +145,8 @@ def reset!
self.host = DEFAULT_HOST
self.remote_addr = "127.0.0.1"
- self.accept = "text/xml,application/xml,application/xhtml+xml," +
- "text/html;q=0.9,text/plain;q=0.8,image/png," +
+ self.accept = "text/xml,application/xml,application/xhtml+xml," \
+ "text/html;q=0.9,text/plain;q=0.8,image/png," \
"*/*;q=0.5"
unless defined? @named_routes_configured
@@ -704,7 +704,7 @@ def test_having_properties_in_around_action
def test_prepending_and_appending_around_action
test_process(MixedFilterController)
- assert_equal " before aroundfilter before procfilter before appended aroundfilter " +
+ assert_equal " before aroundfilter before procfilter before appended aroundfilter " \
" after appended aroundfilter after procfilter after aroundfilter ",
MixedFilterController.execution_log
end
@@ -175,7 +175,7 @@ def test_doesnt_write_session_cookie_if_session_is_not_accessed
def test_doesnt_write_session_cookie_if_session_is_unchanged
with_test_route_set do
- cookies[SessionKey] = "BAh7BjoIZm9vIghiYXI%3D--" +
+ cookies[SessionKey] = "BAh7BjoIZm9vIghiYXI%3D--" \
"fef868465920f415f2c0652d6910d3af288a0367"
get "/no_session_access"
assert_response :success
@@ -35,7 +35,7 @@ def url_for(options = nil) # :nodoc:
when :back
_back_url
else
- raise ArgumentError, "arguments passed to url_for can't be handled. Please require " +
+ raise ArgumentError, "arguments passed to url_for can't be handled. Please require " \
"routes or provide your own implementation"
end
end
@@ -532,11 +532,11 @@ def retrieve_variable(path, as)
[variable, variable_counter, variable_iteration]
end
- IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " +
+ IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " \
"make sure your partial name starts with underscore."
- OPTION_AS_ERROR_MESSAGE = "The value (%s) of the option `as` is not a valid Ruby identifier; " +
- "make sure it starts with lowercase letter, " +
+ OPTION_AS_ERROR_MESSAGE = "The value (%s) of the option `as` is not a valid Ruby identifier; " \
+ "make sure it starts with lowercase letter, " \
"and is followed by any combination of letters, numbers and underscores."
def raise_invalid_identifier(path)
@@ -7,7 +7,7 @@ class Builder
def call(template)
require_engine
- "xml = ::Builder::XmlMarkup.new(:indent => 2);" +
+ "xml = ::Builder::XmlMarkup.new(:indent => 2);" \
"self.output_buffer = xml.target!;" +
template.source +
";xml.target!;"
@@ -45,7 +45,7 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
end
expected = whole_form("/developers/123", "edit_developer_123", "edit_developer", method: "patch") do
- '<input id="developer_projects_attributes_abc_name" name="developer[projects_attributes][abc][name]" type="text" value="project #321" />' +
+ '<input id="developer_projects_attributes_abc_name" name="developer[projects_attributes][abc][name]" type="text" value="project #321" />' \
'<input id="developer_projects_attributes_abc_id" name="developer[projects_attributes][abc][id]" type="hidden" value="321" />'
end
Oops, something went wrong.

4 comments on commit b70fc69

@arunkumarg

Hi @amatsuda ,
I would like to know what is advantage of using \ over + or << operator on string.

thank you

@gsamokovarov
Contributor

+ and << are operations performed at the runtime of the program. When you concat strings with spaces or \ and a newline those are interpreted as one string during the program parsing. This saves the runtime allocations.

@giedriusr

Thanks for explanation.

@arunkumarg

Thank you for explanation @gsamokovarov

Please sign in to comment.