<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users938</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/938" rel="self"/>
  <title>Adam</title>
  <updated>Tue Nov 29 18:56:44 -0800 2011</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor595018</id>
    <published>2011-11-29T18:56:44-08:00</published>
    <title>[Ruby] On Ugly ruby if statement.</title>
    <content type="html">

&lt;pre&gt;@topic = @parent.topic || @parent&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1945-ugly-ruby-if-statement/refactors/595018" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor579050</id>
    <published>2011-10-02T20:03:29-07:00</published>
    <title>[Ruby] On Refactor for table "generator"</title>
    <content type="html">

&lt;pre&gt;#customers
  %table#customerTable
    - @customers.each_slice(5) do |customers|
      %tr
        - customers.each do |customer|
          %td
            = image_tag(customer.logo.url(:thumb))&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1833-refactor-for-table-generator/refactors/579050" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor575874</id>
    <published>2011-09-08T21:46:50-07:00</published>
    <title>[Ruby] On is there a smart way to using "NOT IN" SQL with ActiveRecord?</title>
    <content type="html">

&lt;pre&gt;class Book &amp;lt; ActiveRecord::Base
  def self.excluding(ids)
    ids.blank? ? self : where('id NOT IN (?)', ids)
  end
end

Book.excluding(good_books_ids)&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1809-is-there-a-smart-way-to-using-not-in-sql-with-activerecord/refactors/575874" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor574610</id>
    <published>2011-08-31T14:18:46-07:00</published>
    <title>[Ruby] On Randomize case</title>
    <content type="html">

&lt;pre&gt;def randomize_case(string)
  string.gsub(/./) { |char| char.send(rand(2).zero? ? :downcase : :upcase) }
end&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1778-randomize-case/refactors/574610" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor568713</id>
    <published>2011-07-06T08:47:55-07:00</published>
    <title>[Ruby] On Generate span elements for rating</title>
    <content type="html">

&lt;pre&gt;def rating_stars(rating)
  content_tag(:span, :title =&amp;gt; &amp;quot;#{rating}-stars hotel&amp;quot;) do
    5.times.map { |star| content_tag(:span, '&#9733;', :class =&amp;gt; ('full' if star &amp;lt;= rating.round)) }
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1723-generate-span-elements-for-rating/refactors/568713" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor568712</id>
    <published>2011-07-06T08:43:01-07:00</published>
    <title>[Ruby] On move a element of array to top</title>
    <content type="html">&lt;p&gt;If User happens to be an ActiveRecord class:&lt;/p&gt;

&lt;pre&gt;class User &amp;lt; ActiveRecord::Base
  def self.promote(id)
    # A bound variable would be preferred here, but doesn't seem to be supported by the MySQL adapter.
    # I would recommend sanitizing id for real-world usage.
    order(&amp;quot;id = #{id} DESC&amp;quot;)
  end
end

User.promote(user_id)&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1725-move-a-element-of-array-to-top/refactors/568712" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor568662</id>
    <published>2011-07-06T00:48:49-07:00</published>
    <title>[PHP] On CSS compiler/parser nested sets</title>
    <content type="html">&lt;p&gt;Upon closer inspection, I misinterpreted the format you wanted to use. These modifications should fix that.&lt;/p&gt;

&lt;pre&gt;function selectors() {
	$selectors = preg_split('/\s*,\s*/', $this-&amp;gt;selector);
	
	if ($this-&amp;gt;parent) {
		$mergedSelectors = array();
		$parentSelectors = $this-&amp;gt;parent-&amp;gt;selectors();
		
		foreach ($parentSelectors as $parentSelector) {
			foreach ($selectors as $selector) {
				if ($selector[0] == '&amp;amp;') {
					$selector = substr($selector, 1);
				} else if ($selector[0] != ':') {
					$selector = &amp;quot; $selector&amp;quot;;
				}
				
				array_push($mergedSelectors, &amp;quot;$parentSelector$selector&amp;quot;);
			}
		}
		
		return $mergedSelectors;
	} else {
		return $selectors;
	}
}

function nextIdentifier() {
	if (preg_match('/\s*(:?.*?)\s*([\{\:\;\}])/', $this-&amp;gt;css, &amp;amp;$matches, 0, $this-&amp;gt;offset)) {
		$this-&amp;gt;offset += strlen($matches[0]);		
		return $matches;
	} else {
		return NULL;
	}
}
&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1722-css-compiler-parser-nested-sets/refactors/568662" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor568661</id>
    <published>2011-07-06T00:39:36-07:00</published>
    <title>[PHP] On CSS compiler/parser nested sets</title>
    <content type="html">

&lt;pre&gt;class Block {
	protected $parent;
	protected $selector;
	protected $properties;
	
	function Block($selector) {
		$this-&amp;gt;selector = $selector;
		$this-&amp;gt;children = array();
		$this-&amp;gt;properties = array();
	}
	
	function parent() {
		return $this-&amp;gt;parent;
	}
	
	function setParent($parent) {
		$this-&amp;gt;parent = $parent;
	}
	
	function addChild($child) {
		$child-&amp;gt;setParent($this);
		array_push($this-&amp;gt;children, $child);
	}
	
	function setProperty($name, $value) {
		$this-&amp;gt;properties[$name] = $value;
	}
	
	function selector() {
		return implode(&amp;quot;, &amp;quot;, $this-&amp;gt;selectors());
	}
	
	function selectors() {
		$selectors = preg_split('/\s*,\s*/', $this-&amp;gt;selector);
		
		if ($this-&amp;gt;parent) {
			$mergedSelectors = array();
			$parentSelectors = $this-&amp;gt;parent-&amp;gt;selectors();
			
			foreach ($parentSelectors as $parentSelector) {
				foreach ($selectors as $selector) {
					array_push($mergedSelectors, &amp;quot;$parentSelector$selector&amp;quot;);
				}
			}
			
			return $mergedSelectors;
		} else {
			return $selectors;
		}
	}
	
	function toCSS() {
		$css = &amp;quot;{$this-&amp;gt;selector()} {&amp;quot;;
		
		foreach ($this-&amp;gt;properties as $name =&amp;gt; $value)
			$css .= &amp;quot; $name: $value;&amp;quot;;
			
		$css .= &amp;quot; }\n&amp;quot;;
		
		foreach ($this-&amp;gt;children as $child)
			$css .= $child-&amp;gt;toCSS();
		
		return $css;
	}
}

class Parser {
	protected $css;
	protected $current;
	protected $offset;
	protected $parents;
	
	function Parser($css) {
		$this-&amp;gt;css = $css;
	}
	
	function reset() {
		$this-&amp;gt;current = null;
		$this-&amp;gt;offset = 0;
		$this-&amp;gt;parents = array();
	}
	
	function parse() {
		$this-&amp;gt;reset();
		
		while (list($unused_variable, $identifier, $action) = $this-&amp;gt;nextIdentifier()) {
			switch ($action) {
				case '{':
					$block = new Block($identifier);
					
					if ($this-&amp;gt;current)
						$this-&amp;gt;current-&amp;gt;addChild($block);
					else
						array_push($this-&amp;gt;parents, $block);
						
					$this-&amp;gt;current = $block;					
					break;
					
				case ':':
					$propertyName = $identifier;
					break;
					
				case ';':
					$this-&amp;gt;current-&amp;gt;setProperty($propertyName, $identifier);
					break;
					
				case '}':
					$this-&amp;gt;current = $this-&amp;gt;current-&amp;gt;parent();
					break;
			}
		}
	}
	
	function nextIdentifier() {
		if (preg_match('/\s*(.*?)\s*([\{\:\;\}])/', $this-&amp;gt;css, &amp;amp;$matches, 0, $this-&amp;gt;offset)) {
			$this-&amp;gt;offset += strlen($matches[0]);		
			return $matches;
		} else {
			return NULL;
		}
	}
	
	function toCSS() {
		$css = &amp;quot;&amp;quot;;
		
		foreach ($this-&amp;gt;parents as $parent) {
			$css .= $parent-&amp;gt;toCSS();
		}
		
		return $css;
	}
}

// Example Usage
$parser = new Parser(CSS_DATA);
$parser-&amp;gt;parse();
echo $parser-&amp;gt;toCSS();&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1722-css-compiler-parser-nested-sets/refactors/568661" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor568162</id>
    <published>2011-07-01T12:26:18-07:00</published>
    <title>[JavaScript] On Remove duplicate declarations in jQuery callback</title>
    <content type="html">

&lt;pre&gt;function CategoryDescription(element)
{
	var div = $(element).find('.description');
	var dark = div.siblings('.darkdiv');
	var para = div.find('p');
	var h1 = div.find('h1');
	
	var animate = function(paraOpacity, darkOpacity, extras) {
		return function() {
			extras();
			para.animate({ 'opacity', paraOpacity }, 200);
			dark.animate({ 'opacity', darkOpacity }, 200);
		}
	}
	
	this.show = animate(1, 0, function() { div.animate({'margin-bottom': 0}, 300).animate({'margin-bottom': -3}, 100); });
	this.hide = animate(0, 0.4, function() { animate({'margin-bottom': 5 - div.height() - h1.height() }, 200); });
}&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1721-reduce-duplication-in-jquery-callback/refactors/568162" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor567840</id>
    <published>2011-06-28T11:23:37-07:00</published>
    <title>[Ruby] On find a horizontal line in an image, or consecutive array elements</title>
    <content type="html">

&lt;pre&gt;def find_8_9s(screen)
  match = screen.to_s.match(/9{8}/)
  [ match.offset(0).first / screen.first.size, match.offset(0).first % screen.first.size ] if match
end&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1719-find-a-horizontal-line-in-an-image-or-consecutive-array-elements/refactors/567840" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor567336</id>
    <published>2011-06-24T08:42:46-07:00</published>
    <title>[Ruby] On string concat</title>
    <content type="html">&lt;p&gt;Requires Rails 3.1, but there's really no reason to not be using it for development.&lt;/p&gt;

&lt;pre&gt;//= require jquery
//= require jquery_ujs
//= require jquery-ui&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1714-string-concat/refactors/567336" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor567295</id>
    <published>2011-06-24T00:08:40-07:00</published>
    <title>[Ruby] On rental_has_been_refunded?</title>
    <content type="html">&lt;p&gt;Not true. At least with 3.1, any? does the SELECT COUNT(*) internally. I suppose earlier versions may not.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1713-rental_has_been_refunded/refactors/567295" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor567270</id>
    <published>2011-06-23T18:43:28-07:00</published>
    <title>[Ruby] On rental_has_been_refunded?</title>
    <content type="html">

&lt;pre&gt;def rental_has_been_refunded?
  refunds.where(:transaction_sub_type =&amp;gt; 'rental').any?
end&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1713-rental_has_been_refunded/refactors/567270" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor567163</id>
    <published>2011-06-22T10:43:05-07:00</published>
    <title>[Ruby] On Can I compact multiple gsub calls</title>
    <content type="html">

&lt;pre&gt;@email.body.gsub!(/~(.+?)~/) { @person.send($1.downcase).to_s }&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1711-can-i-compact-multiple-gsub-calls/refactors/567163" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor566946</id>
    <published>2011-06-19T17:28:52-07:00</published>
    <title>[Ruby] On Existence of ActiveRecord attribute</title>
    <content type="html">&lt;p&gt;When you're chaining that many objects together, you're probably approaching it from the wrong point of view. I believe you're trying to do something like this:&lt;/p&gt;

&lt;pre&gt;class User &amp;lt; ActiveRecord::Base
  has_many :orders
  has_many :stores, :through =&amp;gt; :orders
  
  def has_store_url?
    store = stores.order('orders.created_at DESC').first
    store.url.present? if store
  end
end

# For backwards compatibility with your method
def has_store_url?(user)
  user.has_store_url? if user
end&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1707-existence-of-activerecord-attribute/refactors/566946" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor566317</id>
    <published>2011-06-14T20:50:10-07:00</published>
    <title>[Ruby] On Parents</title>
    <content type="html">&lt;p&gt;Seems like a good place to use an enumerator.&lt;/p&gt;

&lt;pre&gt;class Node
  attr_reader :parent
  
  def ancestors(&amp;amp;block)
    if block_given?
      if parent
        block.call(parent)
        parent.ancestors(&amp;amp;block)
      end
    else
      Enumerable::Enumerator.new(self, :ancestors)
    end
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1705-parents/refactors/566317" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor565196</id>
    <published>2011-06-03T14:16:22-07:00</published>
    <title>[ActionScript] On Combining certain attributes in an array into a single string</title>
    <content type="html">

&lt;pre&gt;summary.detentionComments = summary.detentionNotes.map(function(note) { return note.comments }).join(&amp;quot;; &amp;quot;);&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1693-combining-certain-attributes-in-an-array-into-a-single-string/refactors/565196" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor564896</id>
    <published>2011-06-01T08:10:41-07:00</published>
    <title>[PHP] On JSON Literals in PHP</title>
    <content type="html">&lt;p&gt;Good eye. It should have been set in the beginning, but I guess I was able to exploit the fact that switch handles an undefined value as 0 by default as it does work as written.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1690-json-literals-in-php/refactors/564896" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code1690</id>
    <published>2011-05-31T22:13:16-07:00</published>
    <updated>2011-06-01T08:10:43-07:00</updated>
    <title>[PHP] JSON Literals in PHP</title>
    <content type="html">&lt;p&gt;On a forum I was reading, someone wanted to use JSON-style arrays instead of the more verbose PHP-style arrays in a PHP script. I spent a couple of minutes whipping up this PHP preprocessor to be silly, but it turned out to be kind of neat in the end and seemed like a prime target for some refactoring. Have at it!&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
/*
$foo = {
    &amp;quot;bar&amp;quot;: [
        &amp;quot;baz&amp;quot;,
        &amp;quot;refactor&amp;quot;,
        {
            &amp;quot;my&amp;quot;: &amp;quot;code&amp;quot;
        }
    ]
};

Becomes:

$foo = array (
   'bar' =&amp;gt; array (
        0 =&amp;gt; 'baz',
        1 =&amp;gt; 'refactor',
        2 =&amp;gt; array (
           'my' =&amp;gt; 'code',
        )
    )
);
*/

$file = fopen($argv[1], &amp;quot;r&amp;quot;);
$json = &amp;quot;&amp;quot;;
$depth = 0;

while (!feof($file)) {
    $chr = fgetc($file);
    
    switch ($state) {
        case 0:
            echo $chr;
        
            if ($chr == '=' || $chr == '(' || $chr == ',') {
                $state = 1;
            }
            break;
            
        case 1:
            if ($chr == '[' || $chr == '{') {
                $json .= $chr;
                
                $depth = 1;
                $state = 2;
            } else if (preg_match(&amp;quot;/\s/&amp;quot;, $chr)) {
                echo $chr;
            } else {
                echo $chr;
                $state = 0;
            }
            break;
            
        case 2:
            $json .= $chr;
        
            if ($chr == '[' || $chr == '{') {
                $depth++;
            } else if ($chr == ']' || $chr == '}') {
                $depth--;
            }
            
            if ($depth == 0) {
                $code = var_export(json_decode($json), true);
                $code = str_replace(&amp;quot;stdClass::__set_state&amp;quot;, &amp;quot;&amp;quot;, $code);
                echo $code;
                
                $json = &amp;quot;&amp;quot;;
                $state = 0;
            }
            
            break;
    }
}
?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1690-json-literals-in-php" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor564084</id>
    <published>2011-05-25T07:35:20-07:00</published>
    <title>[Ruby] On Test if array contains the same elements of given value</title>
    <content type="html">&lt;p&gt;Great questions. Unsure about the termination, however it worked in all of my tests. Definitely something you'd want to investigate more seriously if used beyond an exercise.&lt;/p&gt;

&lt;p&gt;false in the C API is defined as 0, so it would fail. However, the constraints defined were to find 9 as fast as possible so we're not worried about false. :)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1681-test-if-array-contains-the-same-elements-of-given-value/refactors/564084" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor564001</id>
    <published>2011-05-24T11:58:15-07:00</published>
    <title>[Ruby] On Test if array contains the same elements of given value</title>
    <content type="html">&lt;p&gt;Okay, one more. This is more succinct.&lt;/p&gt;

&lt;p&gt;Benchmarks (100,000 iterations of a 1,000 element array):&lt;/p&gt;

&lt;p&gt;      user     system      total        real
&lt;br /&gt;fast_uniq             0.110000   0.000000   0.110000 (  0.108271)
&lt;br /&gt;ants_is_all_the_same  0.140000   0.000000   0.140000 (  0.147539)
&lt;br /&gt;adam_is_all_the_same  0.060000   0.000000   0.060000 (  0.061935)
&lt;/p&gt;

&lt;pre&gt;VALUE is_all_the_same(VALUE self)
{
	if (RARRAY_LEN(self) &amp;lt; 2)
		return Qtrue;
	
	VALUE *first = RARRAY_PTR(self);
	VALUE *element = first + 1;

	while (*element++ == *first);	
	return *element ? Qfalse : Qtrue;
}&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1681-test-if-array-contains-the-same-elements-of-given-value/refactors/564001" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor564000</id>
    <published>2011-05-24T11:50:17-07:00</published>
    <title>[Ruby] On Test if array contains the same elements of given value</title>
    <content type="html">&lt;p&gt;How come edit doesn't work anymore? Anyway, you need to check that the array is empty if you want to return true in that case. I'll leave that as an exercise for the reader since I can't update it.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1681-test-if-array-contains-the-same-elements-of-given-value/refactors/564000" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor563999</id>
    <published>2011-05-24T11:43:57-07:00</published>
    <title>[Ruby] On Test if array contains the same elements of given value</title>
    <content type="html">&lt;p&gt;This is only slightly faster with short arrays, but the array length grows, this is quite a bit faster on my machine.&lt;/p&gt;

&lt;pre&gt;VALUE is_all_the_same(VALUE self)
{
	VALUE *first = RARRAY_PTR(self);
	VALUE *element = first + 1;

	while (*element++ == *first);	
	return (element - first - 1) == RARRAY_LEN(self) ? Qtrue : Qfalse;
}&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1681-test-if-array-contains-the-same-elements-of-given-value/refactors/563999" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor563916</id>
    <published>2011-05-23T16:41:44-07:00</published>
    <title>[Ruby] On Test if array contains the same elements of given value</title>
    <content type="html">

&lt;pre&gt;input = [9,9,9,9,9,9,9,9,9,9,9,9]
input.fast_uniq(9)

## [C]

#include &amp;lt;ruby.h&amp;gt;

VALUE method_fast_uniq(VALUE self, VALUE num)
{
	int i;
	VALUE *element = RARRAY_PTR(self);
	
	for (i = 0; i &amp;lt; RARRAY_LEN(self); i++) {
		if (*element != num)
			return Qfalse;
		
		element++;
	}
	
	return Qtrue;
}

void Init_fast_uniq()
{
	VALUE array = rb_define_class(&amp;quot;Array&amp;quot;, rb_cObject);
	rb_define_method(array, &amp;quot;fast_uniq&amp;quot;, method_fast_uniq, 1);
}
&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1681-test-if-array-contains-the-same-elements-of-given-value/refactors/563916" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor562447</id>
    <published>2011-05-04T22:28:03-07:00</published>
    <title>[C++] On String parsing in AVR-GCC</title>
    <content type="html">&lt;p&gt;Good points. Apparently I've been spending too much time in Objective-C where instance variables are initialized to zero. :)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Adam</name>
      <email>skidooer+p@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1652-string-parsing-in-avr-gcc/refactors/562447" rel="alternate"/>
  </entry>
</feed>

