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

Ticket #8759 (closed enhancement: fixed)

Opened 1 year ago

Last modified 1 year ago

[PATCH] Array#extract_options! method

Reported by: norbert Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveSupport Version: edge
Severity: normal Keywords: core_ext verified
Cc:

Description

This patch adds a commonly seen snippet as a handy method.

Example:

def options(*args)
  args.extract_options!
end

options(1, 2)           # => {}
options(1, 2, :a => :b) # => {:a=>:b}

Attachments

array_extract_options.diff (1.7 kB) - added by norbert on 06/26/07 10:26:24.
array_extract_options.2.patch (1.7 kB) - added by norbert on 07/15/07 02:14:38.
No more self!?
array_extract_options.3.diff (2.2 kB) - added by norbert on 07/24/07 07:43:47.
... in a separate module

Change History

06/26/07 10:26:24 changed by norbert

  • attachment array_extract_options.diff added.

06/26/07 10:30:32 changed by manfred

I would drop the self}} in the {{{self.last.is_a.

07/15/07 02:08:06 changed by lifofifo

Will be nice to define the extension under ActiveSupport::CoreExtensions::Array::Whatever namespace and include it in array.rb, because conversions/grouping are done that way. If that makes sense in this case.

+1 otherwise.

07/15/07 02:13:55 changed by norbert

Patch without the self coming up.

lifofifo, I think it makes sense this way. It's short enough to just monkeypatch Array directly, and including it in array.rb would clutter the current file with includes.

07/15/07 02:14:38 changed by norbert

  • attachment array_extract_options.2.patch added.

No more self!?

07/19/07 11:56:15 changed by alloy

Amazing that this wasn't already in rails, with all those methods that take options lying around. Seems like a good DRY solution to me.

+1

07/19/07 12:12:37 changed by mpalmer

  • keywords changed from core_ext to core_ext, verified.

Looks solid to me. +1

07/19/07 12:14:05 changed by lifofifo

  • keywords changed from core_ext, verified to core_ext verified.

07/24/07 01:43:11 changed by david

  • keywords changed from core_ext verified to core_ext.

What lifo said. The new standard is never to include stuff directly in the base classes if it's avoidable. It's a lot easier to trace and document additions when done from their own modules. Please do update to follow the new style.

And bonus points for a second patch that goes through Rails and updates it to use this new technique.

Add the verified keyword back when done.

07/24/07 07:42:56 changed by norbert

david, thanks for commenting on this. New patch coming up.

07/24/07 07:43:47 changed by norbert

  • attachment array_extract_options.3.diff added.

... in a separate module

07/24/07 07:55:06 changed by norbert

  • keywords changed from core_ext to core_ext verified.

07/24/07 08:04:49 changed by mpalmer

+1 on the latest patch too. Looks good.

07/24/07 14:17:13 changed by david

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

(In [7217]) Added Array#extract_options! to encapsulate the pattern of getting an options hash out of a variable number of parameters (closes #8759) [norbert]