<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>border: solid 1px red;: Array#inquire</title>
    <link>http://blog.solid1pxred.com/articles/2008/07/01/array-inquire</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>development and other things</description>
    <item>
      <title>Array#inquire</title>
      <description>&lt;pre&gt;&lt;code&gt;
class Array
  def inquire(val)
    include?(val) ? val : first
  end
end
&lt;/code&gt;&lt;/pre&gt;

Not sure if this has been done before, but it should simplify stuff this:

&lt;pre&gt;&lt;code&gt;type = %w(sencha macha kukicha bancha).include?(params[:type]) ? params[:type] : "sencha"&lt;/code&gt;&lt;/pre&gt;

into this:

&lt;pre&gt;&lt;code&gt;type = %w(sencha macha kukicha bancha).inquire(params[:type])&lt;/code&gt;&lt;/pre&gt;

Better names for this than inquire? One I thought of was Array#beg, since you don't always get what you ask for. Hm.

</description>
      <pubDate>Tue, 01 Jul 2008 17:29:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:054a42c4-8aa3-43e1-bfbe-a0ef5d45975e</guid>
      <author>Tieg Zaharia</author>
      <link>http://blog.solid1pxred.com/articles/2008/07/01/array-inquire</link>
      <category>ruby</category>
      <category>ruby on rails</category>
      <category>tea</category>
    </item>
    <item>
      <title>"Array#inquire" by Tieg</title>
      <description>AHA! True, fetch_by_value does make more sense. Defining the default value by hand does add a little more verbosity than originally intended, but it's still better than using a ternary operator imo.
</description>
      <pubDate>Wed, 09 Jul 2008 00:58:25 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1725941d-d93a-4ff6-8c61-d494b4ed50d6</guid>
      <link>http://blog.solid1pxred.com/articles/2008/07/01/array-inquire#comment-737</link>
    </item>
    <item>
      <title>"Array#inquire" by Tim Connor</title>
      <description>Call it fetch_by_value and style it after fetch, for consistency:
&lt;pre&gt;&lt;code&gt;
arr.fetch_by_value(value) -&gt; throws an IndexErrror if value not found
arr.fetch_by_value(value, default) -&gt; default if value not found
arr.fetch_by_value(value) { |v| block } -&gt; calls block with value if not found&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Thu, 03 Jul 2008 16:57:23 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c8e8decd-e0ba-42fe-a46f-ca1014595984</guid>
      <link>http://blog.solid1pxred.com/articles/2008/07/01/array-inquire#comment-736</link>
    </item>
    <item>
      <title>"Array#inquire" by Tieg</title>
      <description>I know! It seems strange. I hesitated for a while, but I've actually noticed this pattern quite a few times (especially with params). Maybe I should have an optional parameter for a default value (or index) that would default to "first" if nil. </description>
      <pubDate>Wed, 02 Jul 2008 03:54:56 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:41762ad6-adfc-4fa4-b696-3d12b1e4b1bf</guid>
      <link>http://blog.solid1pxred.com/articles/2008/07/01/array-inquire#comment-735</link>
    </item>
    <item>
      <title>"Array#inquire" by Tim Connor</title>
      <description>Why?  What is the reason that you want the first one on a miss?</description>
      <pubDate>Tue, 01 Jul 2008 18:15:14 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5bf9a1b0-123e-4ff7-959c-c34462a5af90</guid>
      <link>http://blog.solid1pxred.com/articles/2008/07/01/array-inquire#comment-734</link>
    </item>
  </channel>
</rss>
