Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #4480 (closed defect: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] ActiveSupport: with_options disallows to overwrite an option

Reported by: murphy@cYcnus.de Assigned to: sam@conio.net
Priority: normal Milestone:
Component: ActiveSupport Version:
Severity: normal Keywords: with_options
Cc:

Description

Object#with_options behaves surprisingly to me, as it gives the common options priority over the special ones:

with_options(:hello => 'world') do |foo|
  foo.p(:hello => 'mars')
  # gives: {:hello => "world"}
  # should be: {:hello => "mars"}
end

The simple solution is to merge both options the other way round:

-        arguments << if arguments.last.respond_to? :merge!
-          arguments.pop.dup.merge!(@options)
+        arguments << if arguments.last.respond_to? :to_hash
+          @options.merge(arguments.pop)

(Hash#merge also creates a dup.) We also need to check if the last argument quacks like a Hash, just having #merge! doesn't suffice.

Performance should be the same in both versions.

I'm not sure if the current behaviour was on purpose; I just ran the tests for ActiveSupport successfully.

Patch with test case attached.

Attachments

with_options_allowing_to_overwrite_options.diff (1.5 kB) - added by murphy@cYcnus.de on 03/29/06 12:42:27.
Patch

Change History

03/29/06 12:42:27 changed by murphy@cYcnus.de

  • attachment with_options_allowing_to_overwrite_options.diff added.

Patch

04/03/06 03:37:08 changed by david

  • owner changed from David to sam@conio.net.

04/26/06 21:49:45 changed by marcel

  • status changed from new to closed.
  • resolution set to fixed.

(In [4282]) Allow default options in with_options to be overridden. Closes #4480. [murphy@cYcnus.de]