<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: arrays code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 10 Oct 2008 17:08:47 GMT</pubDate>
    <description>DZone Snippets: arrays code</description>
    <item>
      <title>randomizing an array in ruby (the right way)</title>
      <link>http://snippets.dzone.com/posts/show/4659</link>
      <description>Snagged from http://www.rubyquiz.com/quiz113.html&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# be sure to use sort_by rather than sort&lt;br /&gt;quiz = (1..10).to_a&lt;br /&gt;quiz.sort_by { rand }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 15 Oct 2007 18:09:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4659</guid>
      <author>sikelianos (Zeke Sikelianos)</author>
    </item>
    <item>
      <title>Generic 'sum' and 'mean' methods for Ruby arrays</title>
      <link>http://snippets.dzone.com/posts/show/2161</link>
      <description>&lt;code&gt;class Array; def sum; inject( nil ) { |sum,x| sum ? sum+x : x }; end; end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;It's class agnostic, so you can do this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;[1,2,3].sum              # =&gt; 6&lt;br /&gt;['a','b','c'].sum        # =&gt; 'abc'&lt;br /&gt;[['a'], ['b','c']].sum   # =&gt; ['a', 'b', 'c']&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You can then add a 'mean' operator easily:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;class Array; def mean; sum / size; end; end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;mean only works for numbers though, of course, like so:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;[1,2,1000].mean    # =&gt; 334&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;That said, if your class implements division, it'll also work!</description>
      <pubDate>Thu, 08 Jun 2006 20:19:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2161</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Maybe UnSerialize</title>
      <link>http://snippets.dzone.com/posts/show/1534</link>
      <description>Checks to see if a string is actually the serialized version of an array. If it is, we return an unserialized version. If not, return the original string. This way, we can just call our function on stored DB values and not worry about checking the results.&lt;br /&gt;&lt;br /&gt;This was snagged from the &lt;a href="http://wordpress.org"&gt;Wordpress&lt;/a&gt; 2.0 source. Yayyy for Open Source!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;	function maybe_unserialize ( $original ) {&lt;br /&gt;		if ( false !== $gm = @ unserialize($original) )&lt;br /&gt;			return $gm;&lt;br /&gt;		else&lt;br /&gt;			return $original;&lt;br /&gt;	}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 20 Feb 2006 08:09:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1534</guid>
      <author>MellerTime (Chris Meller)</author>
    </item>
    <item>
      <title>Swap elements of an array in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/280</link>
      <description>&lt;code&gt;class Array&lt;br /&gt;    def swap!(a,b)&lt;br /&gt;         self[a], self[b] = self[b], self[a]&lt;br /&gt;    self&lt;br /&gt;    end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You can now do stuff like..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;[1,2,3,4].swap!(2,3)  # = [1,2,4,3] etc..&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Many thanks to Sam Stephenson and technoweenie for their suggestions.</description>
      <pubDate>Fri, 13 May 2005 09:29:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/280</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
