aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
Commit message (Collapse)AuthorAgeFilesLines
* Fix typos. Improve text_helper documentation.James Lovejoy2018-01-191-4/+4
| | | | [ci skip]
* Enable autocorrect for `Lint/EndAlignment` copKoichi ITO2018-01-181-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ### Summary This PR changes .rubocop.yml. Regarding the code using `if ... else ... end`, I think the coding style that Rails expects is as follows. ```ruby var = if cond a else b end ``` However, the current .rubocop.yml setting does not offense for the following code. ```ruby var = if cond a else b end ``` I think that the above code expects offense to be warned. Moreover, the layout by autocorrect is unnatural. ```ruby var = if cond a else b end ``` This PR adds a setting to .rubocop.yml to make an offense warning and autocorrect as expected by the coding style. And this change also fixes `case ... when ... end` together. Also this PR itself is an example that arranges the layout using `rubocop -a`. ### Other Information Autocorrect of `Lint/EndAlignment` cop is `false` by default. https://github.com/bbatsov/rubocop/blob/v0.51.0/config/default.yml#L1443 This PR changes this value to `true`. Also this PR has changed it together as it is necessary to enable `Layout/ElseAlignment` cop to make this behavior.
* Fix typos, update documentationJames Lovejoy2018-01-113-24/+30
| | | | [ci skip]
* Revert unintentional change in 41e3bbdJavan Makhmali2018-01-011-1/+1
|
* Improve `preventDefault` fix for rails-ujsJavan Makhmali2018-01-011-6/+11
| | | | | | | | Improves 049a3374aa85f33091f0e7cba8635edd4b4786bd: * Attempt native `preventDefault()` before stepping in * Fix that calling `preventDefault()` more than once would throw an error * Fix that non-cancelable events could be canceled
* Bump license years for 2018Yoshiyuki Hirano2017-12-313-3/+3
|
* Remove needless blank lines [ci skip]Ryuta Kamizono2017-12-301-2/+0
| | | | This broke the doc.
* Merge pull request #31578 from ↵George Claghorn2017-12-293-4/+34
|\ | | | | | | | | Aquaj/feature/allow-callables-in-select-group-methods Allow the use of callable objects as group methods for grouped selects.
| * Allow the use of callable objects as group methods for grouped selects.Jérémie Bonal2017-12-273-4/+34
| | | | | | | | | | | | Replaced the uses of `group.send(...)` in `option_groups_from_collection_for_select` by calls to `value_for_collection(group, ...)`, allowing the use of procs, lambdas and other callable objects as parameters.
* | Fix IE 10 and IE 11's broken `preventDefault`Yuri S2017-12-271-0/+5
| | | | | | | | https://github.com/turbolinks/turbolinks/issues/233 https://stackoverflow.com/questions/23349191/event-preventdefault-is-not-working-in-ie-11-for-custom-events
* | Suppress `warning: BigDecimal.new is deprecated`Yasuo Honda2017-12-152-7/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `BigDecimal.new` has been deprecated in BigDecimal 1.3.3 which will be a default for Ruby 2.5. Refer https://github.com/ruby/bigdecimal/commit/533737338db915b00dc7168c3602e4b462b23503 * This commit has been made as follows: ``` cd rails git grep -l BigDecimal.new | grep -v guides/source/5_0_release_notes.md | grep -v activesupport/test/xml_mini_test.rb | xargs sed -i -e "s/BigDecimal.new/BigDecimal/g" ``` - `activesupport/test/xml_mini_test.rb` Editmanually to remove `.new` and `::` - guides/source/5_0_release_notes.md This is a Rails 5.0 release notes.
* | Merge pull request #31289 from witlessbird/fips-compatibilityEileen M. Uchitelle2017-12-141-1/+1
|\ \ | | | | | | Initial support for running Rails on FIPS-certified systems
| * | Introduced `ActiveSupport::Digest` that allows to specify hash function ↵Dmitri Dolguikh2017-12-121-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | implementation and defaults to `Digest::MD5`. Replaced calls to `::Digest::MD5.hexdigest` with calls to `ActiveSupport::Digest.hexdigest`.
* | | Enable `Layout/LeadingCommentSpace` to not allow cosmetic changes in the futureRyuta Kamizono2017-12-143-3/+3
| | | | | | | | | | | | Follow up of #31432.
* | | Enable `Layout/SpaceBeforeComma` rubocop rule, and fixed moreRyuta Kamizono2017-12-123-5/+5
| | | | | | | | | | | | Follow up of #31390.
* | | Merge pull request #30361 from mfo/masterEileen M. Uchitelle2017-12-114-1/+25
|\ \ \ | | | | | | | | StreamingTemplateRenderer fails to forward I18n.locale in layouts
| * | | fix(streaming_template_renderer): I18n.locale broken in layout. I18n gem ↵mfo2017-11-254-1/+25
| | | | | | | | | | | | | | | | stores the current locale in Thread.current[:local] (see: https://github.com/svenfuchs/i18n/blob/master/lib/i18n.rb#L23). StreamingTemplateRenderer is implemented with Fiber which have its own stack of locals and can not access Thread.current.locals(keys, see: https://ruby-doc.org/core-2.2.0/Thread.html#class-Thread-label-Fiber-local+vs.+Thread-local).
* | | | docs: add example for a nil name in link_tomaciej-ka2017-12-071-0/+5
| |_|/ |/| |
* | | Tweaks CHANGELOGs [ci skip]Ryuta Kamizono2017-12-011-1/+1
| |/ |/|
* | Add preload_link_tag helper.Guillermo Iguaran2017-11-283-0/+98
| | | | | | | | | | | | | | | | | | | | | | This helper creates a link tag with preload keyword that allows to browser to initiate early fetch of resources. Additionally this send Early Hints if supported. See https://github.com/rails/rails/pull/30744/commits/59a02fb7bcbe68f26e1e7fdcec45c00c66e4a065 for more details about Early Hints. Preload spec: https://w3c.github.io/preload/
* | Make sure the request exist befoe trying to send early hintsRafael Mendonça França2017-11-282-2/+32
| | | | | | | | | | | | | | Action Mailer context for example responds to request but don't have a a request object. Fixes #31265
* | Preparing for 5.2.0.beta2 releaseRafael Mendonça França2017-11-283-2/+7
| |
* | Fix typos and add a few suggestionsFatos Morina2017-11-283-13/+13
| |
* | Preparing for 5.2.0.beta1 releaseRafael Mendonça França2017-11-273-2/+4
| |
* | Make form_with_generates_ids default value to be falseRafael Mendonça França2017-11-272-1/+10
| | | | | | | | | | This will keep the behavior of an application with the defaults of a 4.2 or 5.0 application behaving the same when upgrading to 5.2.
* | Enable `Style/DefWithParentheses` rubocop ruleRyuta Kamizono2017-11-272-2/+2
|/ | | | | | | The def with blank `()` was newly added in #31176, but we have not used the blank `()` style in most part of our code base. So I've enabled `Style/DefWithParentheses` to prevent to newly added the code.
* Add test to make sure form_for is not affected byRafael Mendonça França2017-11-251-0/+32
| | | | form_with_generates_ids
* Make sure that form_with_generates_ids only affects form_withRafael Mendonça França2017-11-255-3/+9
|
* Make form_with_generates_ids default in Rails 5.2Rafael Mendonça França2017-11-251-3/+1
| | | | | When the defaults being loaded are the 5.0 or 5.1 we disable generation of ids with form_with.
* Change `form_with` to generates ids by defaultnpezza932017-11-259-225/+245
| | | | | | | | | | When `form_with` was introduced we disabled the automatic generation of ids that was enabled in `form_for`. This usually is not an good idea since labels don't work when the input doesn't have an id and it made harder to test with Capybara. You can still disable the automatic generation of ids setting `config.action_view.form_with_generates_ids` to `false.`
* Fix asset url examples [ci skip]Ryuta Kamizono2017-11-151-6/+6
|
* Merge pull request #30963 from ↵Rafael França2017-11-141-1/+14
|\ | | | | | | | | q-centrix/performance-improvements-add_method_to_attributes Performance improvements for add_method_to_attributes!
| * Performance improvements for add_method_to_attributes!Dillon Welch2017-11-071-1/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Prevents two string allocations per method call for common REST verbs plus a ~1.5x speedup for :get in particular ```ruby begin require "bundler/inline" rescue LoadError => e $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" raise e end gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" gem "rails" end def allocate_count GC.disable before = ObjectSpace.count_objects yield after = ObjectSpace.count_objects after.each { |k,v| after[k] = v - before[k] } after[:T_HASH] -= 1 # probe effect - we created the before hash. GC.enable result = after.reject { |k,v| v == 0 } GC.start result end @html_options = {} def master_version if @method && @method.to_s.downcase != "get" && @html_options["rel".freeze] !~ /nofollow/ @html_options["rel".freeze] = "#{@html_options["rel".freeze]} nofollow".lstrip end @html_options["data-method".freeze] = @method end def fast_version if method_not_get_method?(@method) && @html_options["rel".freeze] !~ /nofollow/ @html_options["rel".freeze] = "#{@html_options["rel".freeze]} nofollow".lstrip end @html_options["data-method".freeze] = @method end STRINGIFIED_COMMON_METHODS = { get: 'get', delete: 'delete', patch: 'patch', post: 'post', put: 'put', }.freeze def method_not_get_method?(method) return false unless method (STRINGIFIED_COMMON_METHODS[method] || method.to_s.downcase) != 'get' end puts 'get' @method = :get puts "master_version" puts allocate_count { 1000.times { master_version } } puts "fast_version" puts allocate_count { 1000.times { fast_version } } Benchmark.ips do |x| x.report("master_version") { master_version } x.report("fast_version") { fast_version } x.compare! end puts 'delete' @method = :delete puts "master_version" puts allocate_count { 1000.times { master_version } } puts "fast_version" puts allocate_count { 1000.times { fast_version } } Benchmark.ips do |x| x.report("master_version") { master_version } x.report("fast_version") { fast_version } x.compare! end ``` ``` get master_version {:FREE=>-1819, :T_STRING=>2052} fast_version {:FREE=>-1} Warming up -------------------------------------- master_version 140.839k i/100ms fast_version 175.639k i/100ms Calculating ------------------------------------- master_version 2.683M (± 7.1%) i/s - 13.380M in 5.013447s fast_version 3.988M (±10.1%) i/s - 19.847M in 5.039580s Comparison: fast_version: 3988340.3 i/s master_version: 2683336.2 i/s - 1.49x slower delete master_version {:FREE=>-5003, :T_STRING=>3003, :T_MATCH=>999, :T_IMEMO=>1000} fast_version {:FREE=>-3002, :T_STRING=>1001, :T_MATCH=>1000, :T_IMEMO=>1000} Warming up -------------------------------------- master_version 47.221k i/100ms fast_version 44.153k i/100ms Calculating ------------------------------------- master_version 597.881k (±11.4%) i/s - 2.975M in 5.047200s fast_version 686.014k (±11.6%) i/s - 3.400M in 5.036564s Comparison: fast_version: 686014.5 i/s master_version: 597881.4 i/s - same-ish: difference falls within error ```
* | Use new Rake URL of Ruby core [ci skip]aycabta2017-11-101-1/+1
| |
* | Merge pull request #31088 from neumayr/am-helper-missing-field_with_errorsRafael França2017-11-094-3/+104
|\ \ | | | | | | field_error_proc wrap form select optgroup and separator option
| * | Fix field_error_proc wrap form select optgroup and divider option tagneumayr2017-11-094-3/+104
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ### Summary The [`:field_error_proc`](https://github.com/rails/rails/blob/master/actionview/lib/action_view/base.rb#L145) is responsible for decorating input tags that refer to attributes with errors. This default build-in rails feature wrap invalid form elements with additional markup: `<div class="field_with_errors">[…]</div>`. * Fix for `field_error_proc` wraps form select `optgroup` * Fix for `field_error_proc` wraps form select divider `option` * Add tests for uncovered elements with errors [Fixes #31088] #### Test coverage * `test_select_grouped_options_with_errors` * `test_time_zone_select_with_priority_zones_and_errors` #### Extend test coverage * `test_collection_select_with_errors` * `test_label_with_errors` * `test_check_box_with_errors` * `test_check_boxes_with_errors` * `test_radio_button_with_errors` * `test_radio_buttons_with_errors` * `test_collection_check_boxes_with_errors` * `test_collection_radio_buttons_with_errors`
* | | Correctly kill the server started with ujs testyuuji.yaginuma2017-11-091-2/+2
|/ / | | | | | | | | | | | | | | | | | | `Kernel.#spawn` execute command via the shell if contains shell metacharacters in the command. In that case, return value of `spawn` is pid of the shell, not the server. Therefore, just killing the pid will leave the process of server. In order to correctly kill the server, send a signal to the process group, not the process.
* / Fix output of `select_tag` with `include_blank: true` [ci skip]yuuji.yaginuma2017-11-081-1/+1
|/ | | | | Since #24923, if use `select_tag` with `include_blank: true`, an empty label is added.
* Merge remote-tracking branch 'upstream/master' into nofollow-changeDillon Welch2017-11-038-10/+33
|\
| * Fix "warning: instance variable @defined_root not initialized"yuuji.yaginuma2017-11-021-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently, the following error is shows only when run the test using `bin/test`. ``` ./bin/test -w test/template/log_subscriber_test.rb Run options: --seed 17167 # Running: /rails/actionview/test/template/log_subscriber_test.rb:34: warning: instance variable @defined_root not initialized ``` In `AVLogSubscriberTest`, if the `Rails.root` is not defined, define the method and undef it in teardown. https://github.com/rails/rails/blob/master/actionview/test/template/log_subscriber_test.rb#L21..L33 However, in `bin/test`, `Rails.root` is defined, which results in referring to uninitialized variables and warnings.
| * Merge pull request #31004 from shuheiktgw/remove_unnecessary_returnsRafael França2017-10-311-2/+2
| |\ | | | | | | Remove redundant return statements
| | * removed unnecessary returnsShuhei Kitagawa2017-10-281-2/+2
| | |
| * | Merge pull request #31012 from y-yagi/remove_unused_missing_requests_errorRafael França2017-10-302-4/+0
| |\ \ | | | | | | | | Remove unused `MissingRequestError`
| | * | Remove unused `MissingRequestError`yuuji.yaginuma2017-10-312-4/+0
| | |/ | | | | | | | | | `MissingRequestError` is no longer used since 1e2b0ce.
| * | Merge pull request #31002 from ta1kt0me/call_ajax_without_beforeSendGuillermo Iguaran2017-10-293-2/+29
| |\ \ | | | | | | | | Call Rails.ajax without beforeSend
| | * | Enable to call Rails.ajax without beforeSendta1kt0me2017-10-283-2/+29
| | |/
| * / Merge pull request #31005 from shuheiktgw/remove_unnecessary_semicolonsMatthew Draper2017-10-281-1/+1
| |/ | | | | | | Removed unnecessary semicolons
* | Fix typoDillon Welch2017-11-021-1/+1
| |
* | Use blank? check instead of key? checkDillon Welch2017-11-021-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This allows us to prevent an extra string allocation when there is a rel argument and performs better/within error of the key check for other scenarios such as passing in rel: nil ```ruby begin require "bundler/inline" rescue LoadError => e $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" raise e end gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" gem "rails" end def allocate_count GC.disable before = ObjectSpace.count_objects yield after = ObjectSpace.count_objects after.each { |k,v| after[k] = v - before[k] } after[:T_HASH] -= 1 # probe effect - we created the before hash. GC.enable result = after.reject { |k,v| v == 0 } GC.start result end @hash = {} def master_version "#{@hash["rel"]} nofollow".lstrip end def key_version if @hash.key?("rel") "#{@hash["rel"]} nofollow".lstrip else "nofollow" end end def present_version if @hash["rel"].present? "#{@hash["rel"]} nofollow" else "nofollow".freeze end end def nil_version if @hash["rel"].nil? "nofollow".freeze else "#{@hash["rel"]} nofollow" end end def blank_version if @hash["rel"].blank? "nofollow".freeze else "#{@hash["rel"]} nofollow" end end def test puts "master_version" puts allocate_count { 1000.times { master_version } } puts "key_version" puts allocate_count { 1000.times { key_version } } puts "present_version" puts allocate_count { 1000.times { present_version } } puts "nil_version" puts allocate_count { 1000.times { nil_version } } puts "blank_version" puts allocate_count { 1000.times { blank_version } } Benchmark.ips do |x| x.report("master_version") { master_version } x.report("key_version") { key_version } x.report("present_version") { present_version } x.report("nil_version") { nil_version } x.report("blank_version") { blank_version } x.compare! end end puts 'no rel key' test puts 'rel key with real stuff' @hash['rel'] = 'hi'.freeze test puts 'rel key with nil' @hash['rel'] = nil test puts 'rel key with ""' @hash['rel'] = "" test ``` ``` no rel key master_version {:FREE=>-2818, :T_STRING=>3052} key_version {:FREE=>-1} present_version {:FREE=>-1} nil_version {:FREE=>-1} blank_version {:FREE=>-1} Warming up -------------------------------------- master_version 124.677k i/100ms key_version 227.992k i/100ms present_version 208.068k i/100ms nil_version 235.272k i/100ms blank_version 176.274k i/100ms Calculating ------------------------------------- master_version 1.968M (±10.8%) i/s - 9.725M in 5.010763s key_version 7.734M (±11.2%) i/s - 38.075M in 5.001613s present_version 5.688M (±11.4%) i/s - 28.089M in 5.019560s nil_version 6.965M (±10.2%) i/s - 34.585M in 5.024806s blank_version 6.139M (±18.7%) i/s - 29.085M in 5.010919s Comparison: key_version: 7734058.3 i/s nil_version: 6965050.2 i/s - same-ish: difference falls within error blank_version: 6138744.3 i/s - same-ish: difference falls within error present_version: 5688248.4 i/s - 1.36x slower master_version: 1967932.3 i/s - 3.93x slower rel key with real stuff master_version {:FREE=>-2001, :T_STRING=>2000} key_version {:FREE=>-2001, :T_STRING=>2000} present_version {:FREE=>-1001, :T_STRING=>1000} nil_version {:FREE=>-1002, :T_STRING=>1000, :T_IMEMO=>1} blank_version {:FREE=>-1001, :T_STRING=>1000} Warming up -------------------------------------- master_version 93.351k i/100ms key_version 89.747k i/100ms present_version 91.963k i/100ms nil_version 103.370k i/100ms blank_version 74.845k i/100ms Calculating ------------------------------------- master_version 2.179M (±21.4%) i/s - 10.362M in 5.044668s key_version 2.345M (± 9.8%) i/s - 11.667M in 5.030982s present_version 1.738M (±14.8%) i/s - 8.553M in 5.056406s nil_version 2.485M (±19.1%) i/s - 11.888M in 5.015940s blank_version 1.951M (±12.3%) i/s - 9.580M in 5.011932s Comparison: nil_version: 2484704.1 i/s key_version: 2344664.8 i/s - same-ish: difference falls within error master_version: 2178975.8 i/s - same-ish: difference falls within error blank_version: 1950532.0 i/s - same-ish: difference falls within error present_version: 1737866.7 i/s - 1.43x slower rel key with nil master_version {:FREE=>-3001, :T_STRING=>3000} key_version {:FREE=>-3001, :T_STRING=>3000} present_version {:FREE=>-1} nil_version {:FREE=>-1} blank_version {:FREE=>-1} Warming up -------------------------------------- master_version 112.655k i/100ms key_version 105.048k i/100ms present_version 136.219k i/100ms nil_version 192.026k i/100ms blank_version 184.846k i/100ms Calculating ------------------------------------- master_version 1.893M (±12.6%) i/s - 9.238M in 5.002621s key_version 1.672M (±13.5%) i/s - 8.194M in 5.021197s present_version 4.484M (±20.5%) i/s - 21.114M in 5.002982s nil_version 5.294M (±18.1%) i/s - 25.155M in 5.020721s blank_version 5.588M (± 6.7%) i/s - 27.912M in 5.019305s Comparison: blank_version: 5588489.6 i/s nil_version: 5293929.9 i/s - same-ish: difference falls within error present_version: 4484493.7 i/s - same-ish: difference falls within error master_version: 1892919.0 i/s - 2.95x slower key_version: 1672343.9 i/s - 3.34x slower rel key with "" master_version {:FREE=>-2001, :T_STRING=>2000} key_version {:FREE=>-2001, :T_STRING=>2000} present_version {:FREE=>-1} nil_version {:FREE=>-1001, :T_STRING=>1000} blank_version {:FREE=>-1} Warming up -------------------------------------- master_version 140.499k i/100ms key_version 124.738k i/100ms present_version 186.659k i/100ms nil_version 148.063k i/100ms blank_version 178.707k i/100ms Calculating ------------------------------------- master_version 1.826M (±24.2%) i/s - 8.289M in 5.026603s key_version 1.561M (±15.3%) i/s - 7.609M in 5.005662s present_version 3.622M (±19.9%) i/s - 17.173M in 5.042217s nil_version 2.438M (±11.5%) i/s - 12.141M in 5.053335s blank_version 4.911M (±15.5%) i/s - 23.768M in 5.009106s Comparison: blank_version: 4910741.1 i/s present_version: 3622183.5 i/s - same-ish: difference falls within error nil_version: 2437606.2 i/s - 2.01x slower master_version: 1825652.2 i/s - 2.69x slower key_version: 1560530.5 i/s - 3.15x slower ```
* | Prevent extra string allocations when no 'rel' arg passedDillon Welch2017-10-261-3/+7
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Do a check if the 'rel' argument is passed in, and simply set it to 'nofollow' if 'rel' was not passed in. This prevents three string allocations for each call to `link_to` in that scenario. In the scenario where the 'rel' argument is passed in, performance is around the same as before as the `key?` check is very fast. ```ruby begin require "bundler/inline" rescue LoadError => e $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" raise e end gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" gem "rails" end def allocate_count GC.disable before = ObjectSpace.count_objects yield after = ObjectSpace.count_objects after.each { |k,v| after[k] = v - before[k] } after[:T_HASH] -= 1 # probe effect - we created the before hash. GC.enable result = after.reject { |k,v| v == 0 } GC.start result end @hash = {} def master_version "#{@hash['rel'.freeze]} nofollow".lstrip end def fast_version if @hash.key?('rel'.freeze) "#{@hash["rel"]} nofollow".lstrip else "nofollow".freeze end end puts 'no rel key' puts "master_version" puts allocate_count { 1000.times { master_version } } puts "fast_version" puts allocate_count { 1000.times { fast_version } } Benchmark.ips do |x| x.report("master_version") { master_version } x.report("fast_version") { fast_version } x.compare! end puts 'rel key' @hash['rel'] = 'hi'.freeze puts "master_version" puts allocate_count { 1000.times { master_version } } puts "fast_version" puts allocate_count { 1000.times { fast_version } } Benchmark.ips do |x| x.report("master_version") { master_version } x.report("fast_version") { fast_version } x.compare! end ``` ``` no rel key master_version {:FREE=>-2791, :T_STRING=>3052} fast_version {:FREE=>-1} Warming up -------------------------------------- master_version 80.324k i/100ms fast_version 200.262k i/100ms Calculating ------------------------------------- master_version 2.049M (±11.9%) i/s - 10.121M in 5.025613s fast_version 6.645M (±21.3%) i/s - 29.439M in 5.007488s Comparison: fast_version: 6644506.3 i/s master_version: 2048833.0 i/s - 3.24x slower rel key master_version {:FREE=>-2001, :T_STRING=>2000} fast_version {:FREE=>-2001, :T_STRING=>2000} Warming up -------------------------------------- master_version 155.673k i/100ms fast_version 106.515k i/100ms Calculating ------------------------------------- master_version 2.652M (±20.4%) i/s - 12.610M in 5.036494s fast_version 2.237M (±16.8%) i/s - 10.865M in 5.035366s Comparison: master_version: 2651702.2 i/s fast_version: 2237470.6 i/s - same-ish: difference falls within error ```