<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Lucid &#187; reject</title>
	<atom:link href="http://thelucid.com/tag/reject/feed/" rel="self" type="application/rss+xml" />
	<link>http://thelucid.com</link>
	<description>The Lightweight Ramblings of Jamie Hill</description>
	<lastBuildDate>Thu, 26 Jan 2012 13:52:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Ruby reject! that returns the rejected items</title>
		<link>http://thelucid.com/2007/07/25/a-ruby-reject-that-returns-the-rejected-items/</link>
		<comments>http://thelucid.com/2007/07/25/a-ruby-reject-that-returns-the-rejected-items/#comments</comments>
		<pubDate>Wed, 25 Jul 2007 14:48:00 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[reject]]></category>

		<guid isPermaLink="false">http://76591e7d-a15e-45ac-a512-a0548e18e88d</guid>
		<description><![CDATA[I often need to do a reject! and return the rejected items instead of the modified collection. This saves having to do a select beforehand. An example of how accomplish this: options = &#123; :a =&#62; 1, :b =&#62; 2, :c =&#62; 3 &#125; rejects = Hash&#91;*options.select &#123; &#124;k, v&#124; k == :b &#38;&#38; options.delete&#40;k&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>I often need to do a <code>reject!</code> and return the rejected items instead of the modified collection. This saves having to do a <code>select</code> beforehand.</p>
<p>An example of how accomplish this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">options = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:a</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span>, <span style="color:#ff3333; font-weight:bold;">:c</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
rejects = <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">*</span>options.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>k, v<span style="color:#006600; font-weight:bold;">|</span> k == <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> options.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>k<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
assert_equal <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:a</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#ff3333; font-weight:bold;">:c</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, options
assert_equal <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, rejects</pre></div></div>

<p>This could be written as a method of the <code>Hash</code> class and an alternative for <code>Array</code>.</p>
<p>For <code>Hash</code>, the code would look something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Hash</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> extract!
    <span style="color:#CC00FF; font-weight:bold;">Hash</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>k, v<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#9966CC; font-weight:bold;">yield</span><span style="color:#006600; font-weight:bold;">&#40;</span>k, v<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>k<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">flatten</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
options = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:a</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span>, <span style="color:#ff3333; font-weight:bold;">:c</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
rejects = options.<span style="color:#9900CC;">extract</span>! <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>k, v<span style="color:#006600; font-weight:bold;">|</span> k == <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
assert_equal <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:a</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">1</span>, <span style="color:#ff3333; font-weight:bold;">:c</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, options
assert_equal <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, rejects</pre></div></div>

<p>If I am missing something obvious in Ruby that accomplishes the same, please leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2007/07/25/a-ruby-reject-that-returns-the-rejected-items/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

