<?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:users1727</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1727" rel="self"/>
  <title>Ben Marini</title>
  <updated>Sun Oct 23 19:18:26 -0700 2011</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor583403</id>
    <published>2011-10-23T19:18:26-07:00</published>
    <title>[Ruby] On apply a credit over an ordered set of tuples</title>
    <content type="html">&lt;p&gt;Recursion seems to fit well here.&lt;/p&gt;

&lt;pre&gt;def apply_credit(credit, tuple)
  num = tuple.shift

  if num.nil?
    [ ]
  elsif num &amp;gt; credit
    [ num - credit ] + tuple
  else
    [ 0 ] + apply_credit(credit - num, tuple)
  end
end

if $0 == __FILE__
  require &amp;quot;test/unit&amp;quot;

  class TestBonus &amp;lt; Test::Unit::TestCase
    def test_case_name
      # example: if the credit is $10 and the row amounts are ($2, $6, $5),
      # it should adjust the row amounts to ($0, $0, $3)

      assert_equal [0, 0, 3], apply_credit(10, [2, 6, 5])
    end
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1904-apply-a-credit-over-an-ordered-set-of-tuples/refactors/583403" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor578810</id>
    <published>2011-10-01T12:56:54-07:00</published>
    <title>[Ruby] On if/else with code duplication</title>
    <content type="html">&lt;p&gt;Why not let your subclasses decide what options to add?&lt;/p&gt;

&lt;pre&gt;class Animal
  def queue_event
    with_new_options do
      $options &amp;lt;&amp;lt; self.options
      Event.queue self, :new_video, { :object_key =&amp;gt; self._object_key, :object_data =&amp;gt; self._render }
    end
  end
end

class Dog &amp;lt; Animal
  def options
    :Dog_options
  end
end

class Cat &amp;lt; Animal
  def options
    :Cat_options
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1830-if-else-with-code-duplication/refactors/578810" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor566321</id>
    <published>2011-06-14T21:27:51-07:00</published>
    <title>[Ruby] On Parents</title>
    <content type="html">&lt;p&gt;Here's one way&lt;/p&gt;

&lt;pre&gt;class Node &amp;lt; Struct.new(:name, :parent)
  def ancestors
    parent.nil? ? [] : [ parent ] + parent.ancestors
  end

  def to_s
    name
  end
end

progenitor          = Node.new('progenitor')
grand_grand_parent  = Node.new('grand_grand_parent', progenitor)
grand_parent        = Node.new('grand_parent', grand_grand_parent)
parent              = Node.new('parent', grand_parent)
node                = Node.new('node', parent)

puts node.ancestors.join(&amp;quot;, &amp;quot;)&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1705-parents/refactors/566321" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor564044</id>
    <published>2011-05-24T21:22:42-07:00</published>
    <title>[Ruby] On Inject all filenames of a specific directory in one line</title>
    <content type="html">

&lt;pre&gt;FILENAMES = Dir[&amp;quot;a/certain/dir/*&amp;quot;]&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1684-inject-all-filenames-of-a-specific-directory-in-one-line/refactors/564044" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor560282</id>
    <published>2011-04-07T15:10:37-07:00</published>
    <title>[Ruby] On Messy Blocks</title>
    <content type="html">&lt;p&gt;My progressive refactoring, final is at the bottom.&lt;/p&gt;

&lt;pre&gt;def disease_search(query)
  treatment_results = Array.new
  symptom_results = Array.new
  image_results = Array.new
  note_results = Array.new

  disease_results = Disease.search(&amp;quot;#{query} user_id:#{current_user.id}&amp;quot;)

  Treatment.search(&amp;quot;#{query}&amp;quot;).each do |treatment|
    treatment_results.push(treatment.diseases)
  end

  Symptom.search(&amp;quot;#{query}&amp;quot;).each do |symptom|
    symptom_results.push(symptom.disease)
  end

  Note.search(&amp;quot;#{query}&amp;quot;).each do |note|
    note_results.push(note.disease)
  end

  Image.search(&amp;quot;#{query}&amp;quot;).each do |image|
    image_results.push(image.disease)
  end

  search_results = (disease_results.concat(treatment_results).concat(symptom_results).concat(image_results).concat(note_results)).uniq
  return search_results
end

def disease_search(query)
  disease_results   = Disease.search(&amp;quot;#{query} user_id:#{current_user.id}&amp;quot;)
  treatment_results = Treatment.search(&amp;quot;#{query}&amp;quot;).map { |t| t.diseases }
  symptom_results   = Symptom.search(&amp;quot;#{query}&amp;quot;).map { |s| s.disease }
  note_results      = Note.search(&amp;quot;#{query}&amp;quot;).map { |n| n.disease }
  image_results     = Image.search(&amp;quot;#{query}&amp;quot;).map { |i| i.disease }

  search_results = (disease_results.concat(treatment_results).concat(symptom_results).concat(image_results).concat(note_results)).uniq
  return search_results
end

def disease_search(query)
  results  = []
  results += Disease.search(&amp;quot;#{query} user_id:#{current_user.id}&amp;quot;)
  results += Treatment.search(&amp;quot;#{query}&amp;quot;).map { |t| t.diseases }
  results += Symptom.search(&amp;quot;#{query}&amp;quot;).map { |s| s.disease }
  results += Note.search(&amp;quot;#{query}&amp;quot;).map { |n| n.disease }
  results += Image.search(&amp;quot;#{query}&amp;quot;).map { |i| i.disease }
  results.uniq
end

require 'set'
def disease_search(query)
  results = Set.new
  results.merge Disease.search(&amp;quot;#{query} user_id:#{current_user.id}&amp;quot;)
  results.merge Treatment.search(&amp;quot;#{query}&amp;quot;).map { |t| t.diseases }
  results.merge Symptom.search(&amp;quot;#{query}&amp;quot;).map { |s| s.disease }
  results.merge Note.search(&amp;quot;#{query}&amp;quot;).map { |n| n.disease }
  results.merge Image.search(&amp;quot;#{query}&amp;quot;).map { |i| i.disease }
  results
end
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1639-messy-blocks/refactors/560282" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor558648</id>
    <published>2011-03-16T05:14:04-07:00</published>
    <title>[Ruby] On Clear a hash of empty string values recursively</title>
    <content type="html">

&lt;pre&gt;class String
  def blank?
    self == &amp;quot;&amp;quot;
  end
end

class Hash
  def blank?
    self == {}
  end

  def remove_blanks
    inject({}) do |mem,(key,val)|
      case val
      when String
        mem[key] = val
      when Hash
        mem[key] = val.remove_blanks
      end unless val.blank?

      mem
    end
  end
end

require &amp;quot;test/unit&amp;quot;

class TestHash &amp;lt; Test::Unit::TestCase
  def test_hash
    h = {
      'a' =&amp;gt; '',
      'b' =&amp;gt; {
        'c' =&amp;gt; '',
        'd' =&amp;gt; 'e'
      }
    }

    expected = { 'b' =&amp;gt; { 'd' =&amp;gt; 'e' } }
    assert_equal expected, h.remove_blanks
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1616-clear-a-hash-of-empty-string-values-recursively/refactors/558648" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor545639</id>
    <published>2010-11-29T02:32:39-08:00</published>
    <title>[Ruby] On Event: set end time based on length</title>
    <content type="html">&lt;p&gt;My thought is the interface to this class is confusing...try to simplify it by imposing some restrictions.&lt;/p&gt;

&lt;pre&gt;# * Create a factory method for creating events. Make length required.
# * Require start and end times for persistence.
# * Don't allow length to be changed. It is confusing...should the start time
#   or end time change when length changes?
class Event &amp;lt; ActiveRecord::Base
  validates_presence_of :start_time, :end_time

  def self.from_start_time_and_length(start_time, length)
    self.start_time = start_time
    self.end_time   = start_time + length.minutes
  end

  def length
    @length ||= ( self.end_time - self.start_time ) / 60
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1504-event-set-end-time-based-on-length/refactors/545639" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor538369</id>
    <published>2010-10-18T15:36:17-07:00</published>
    <title>[Ruby] On Triangles</title>
    <content type="html">

&lt;pre&gt;class TriangleError &amp;lt; StandardError; end

def triangle(a, b, c)
  if [a,b,c].any? { |e| e &amp;lt;= 0 }
    raise TriangleError, &amp;quot;Wrong length&amp;quot;
  end

  if [ [a,b+c], [b,a+c], [c,a+b] ].any? { |(side, sum)| side &amp;gt;= sum }
    raise TriangleError, &amp;quot;Too small&amp;quot;
  end

  case [a,b,c].uniq.length
  when 1 then :equilateral
  when 2 then :isosceles
  else;       :scalene
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1452-triangles/refactors/538369" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor520564</id>
    <published>2010-07-18T20:56:10-07:00</published>
    <title>[Ruby] On instance extending problem</title>
    <content type="html">&lt;p&gt;String is not outputting the way you want because `#to_yaml_properties` (called in `String#to_yaml`) is detecting the  `prefix` accessor. Here is one way around that...&lt;/p&gt;

&lt;pre&gt;module Actions
  attr_accessor :prefix

  def to_yaml
    prefix + super
  end

  def to_yaml_style
    :inline
  end

  def to_yaml_properties
    []
  end
end # Actions
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1355-instance-extending-problem/refactors/520564" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor490013</id>
    <published>2010-03-31T00:27:50-07:00</published>
    <title>[Ruby] On group objects in array</title>
    <content type="html">&lt;p&gt;You don't need to do a select inside the group_by. The way #group_by works is it groups based on the return value of the block you pass it. Try it this way and see how it performs:&lt;/p&gt;

&lt;pre&gt;res = arr.group_by { |f| [f.status_id, f.profile_id] }.values&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1244-group-objects-in-array/refactors/490013" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor489314</id>
    <published>2010-03-29T15:13:38-07:00</published>
    <title>[Ruby] On group objects in array</title>
    <content type="html">&lt;p&gt;#group_by does what you need, you just need to grab the values of the hash afterward&lt;/p&gt;

&lt;pre&gt;require 'rubygems'
require 'active_support'
Foo = Struct.new(:id, :status_id, :profile_id)
arr = [Foo.new(1,2,3),Foo.new(2,2,3),Foo.new(3,1,3)]
res = arr.group_by(&amp;amp;:status_id).values
p res
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1244-group-objects-in-array/refactors/489314" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor436240</id>
    <published>2010-02-03T16:09:53-08:00</published>
    <title>[Ruby] On Symplify few loops</title>
    <content type="html">&lt;p&gt;Here's my functional version: &lt;a href="http://gist.github.com/293713" target="_blank"&gt;http://gist.github.com/293713&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1163-symplify-few-loops/refactors/436240" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor436239</id>
    <published>2010-02-03T16:09:09-08:00</published>
    <title>[Ruby] On Symplify few loops</title>
    <content type="html">&lt;p&gt;Here's my functional version, with some test code.&lt;/p&gt;

&lt;pre&gt;module Bitmap
  def self.bgr_to_rgba(raw_bgr_array, width, height, new_dimension)
    width_padding  = new_dimension - width
    height_padding = new_dimension - height

    # First convert to array of Pixel::BGR objects
    raw_bgr_array.enum_for(:each_slice, 3).map { |a| Pixel::BGR.new(*a) }.
      reverse. # Reverse it
      map { |bgr| bgr.to_rgba }. # Map to array of Pixel::RGBA objects
      enum_for(:each_slice, width).inject([]) { |memo, row| memo &amp;lt;&amp;lt; row }. # Create a 2d array based on width
      map { |row|
        row.concat( [Pixel::RGBA.blank] * width_padding )
      }.concat( [ [Pixel::RGBA.blank] * new_dimension ] * height_padding ). # Change dimensions of 2d array, adding blanks
      flatten.map { |rgba| rgba.to_a }.flatten # Flatten everything back into an array of integers
  end

  module Pixel
    class RGBA &amp;lt; Struct.new(:red, :green, :blue, :alpha)
      def self.blank
        new(0,0,0,0)
      end

      def to_a
        [red, green, blue, alpha]
      end
    end

    class BGR &amp;lt; Struct.new(:blue, :green, :red)
      def to_rgba
        RGBA.new(red, green, blue, 255)
      end
    end
  end
end

if __FILE__ == $0
  require &amp;quot;test/unit&amp;quot;
  class TestBgrToRgba &amp;lt; Test::Unit::TestCase
    def test_bgr_to_rgba
      width     = 2
      height    = 3
      real_size = 4 # closest power of 2

      data = [
        255, 0, 0, # 1st pixel, blue (third row)
        255, 0, 0, # 2st pixel, blue
        
        0, 255, 0, # 3nd pixel, green (second row)
        0, 255, 0, # 4nd pixel, green
        
        0, 0, 255, # 5rd pixel, red (first row)
        0, 0, 255, # 6rd pixel, red
      ]

      corrected = [
        255, 0, 0, 255, # 1st pixel, red (first row)
        255, 0, 0, 255, # 2st pixel, red
        0, 0, 0, 0,     # Empty
        0, 0, 0, 0,     # Empty
        
        0, 255, 0, 255, # 3nd pixel, green (second row)
        0, 255, 0, 255, # 4nd pixel, green
        0, 0, 0, 0,     # Empty
        0, 0, 0, 0,     # Empty
        
        0, 0, 255, 255, # 5rd pixel, blue (third row)
        0, 0, 255, 255, # 6rd pixel, blue
        0, 0, 0, 0,     # Empty
        0, 0, 0, 0,     # Empty
        
        0, 0, 0, 0,     # Empty (last row)
        0, 0, 0, 0,     # Empty
        0, 0, 0, 0,     # Empty
        0, 0, 0, 0      # Empty
      ]

      result = Bitmap.bgr_to_rgba(data, width, height, real_size)
      assert_equal corrected, result
    end
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1163-symplify-few-loops/refactors/436239" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor432497</id>
    <published>2010-01-31T20:33:21-08:00</published>
    <title>[Ruby] On Symplify few loops</title>
    <content type="html">&lt;p&gt;I'd like to help out, but I can't quite figure out what your code snippet is doing. Can you provide a more complete code snippet? For instance, define `height`, `width`, `real_size`, `data`, and `corrected`?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1163-symplify-few-loops/refactors/432497" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor380046</id>
    <published>2009-12-08T08:13:42-08:00</published>
    <title>[PHP] On How should I optimize this for speed?</title>
    <content type="html">&lt;p&gt;Your benchmarking code is benchmarking more than just your Collection class. It's also benchmarking microtime() and more importantly, mt_rand(). You should isolate your code in the benchmark. With the code below, I was getting roughly 97,951 writes per second on my 2.4Ghz MacBook. I got was able to speed that up 150% (150,428 writes per sec) by replacing your while loops with foreach loops in the data function.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
  // Benchmarks for writes and reads, separated.
  $times = 10000;
  $set   = new Collection();
  $start = microtime(true);
  for ($i=0; $i &amp;lt; $times; $i++) { 
    $set-&amp;gt;data('random.numbers.put.in.a.really.deeply.nested.array', 5); // Write
  }
  $diff = microtime(true) - $start;
  echo &amp;quot;Writes, long key\n&amp;quot;;
  echo &amp;quot;$times writes in $diff\n&amp;quot;;
  echo $times / $diff . &amp;quot; writes/sec\n\n&amp;quot;;

  $times = 10000;
  $set   = new Collection();
  $set-&amp;gt;data('random.numbers.put.in.a.really.deeply.nested.array', 5);
  $start = microtime(true);
  for ($i=0; $i &amp;lt; $times; $i++) { 
    $set-&amp;gt;data('random.numbers.put.in.a.really.deeply.nested.array'); // Write
  }
  $diff = microtime(true) - $start;
  echo &amp;quot;Reads, long key\n&amp;quot;;
  echo &amp;quot;$times reads in $diff\n&amp;quot;;
  echo $times / $diff . &amp;quot; reads/sec\n\n&amp;quot;;

  // Data function with while loops replaced with foreach loops

    public function data($key,$value=null) {
      $keys = explode('.', $key);
      $ptr  = &amp;amp;$this-&amp;gt;data;
      if ( !is_null($value) ) {
        foreach ($keys as $key) {
          if ( !isset($ptr[$key]) ) $ptr[$key] = array();
          $ptr = &amp;amp;$ptr[$key];
        }

        $ptr[] = $value;
        return true;
      } else {
        foreach ($keys as $key) {
          $ptr = &amp;amp;$ptr[$key];
        }
        return array_shift($ptr);
      }
    }

&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1123-how-should-i-optimize-this-for-speed/refactors/380046" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor374344</id>
    <published>2009-11-30T17:26:43-08:00</published>
    <title>[Ruby] On Can this be less clunky?</title>
    <content type="html">&lt;p&gt;I'm with Daniel, the source of the problem is the confusing hash, which is
&lt;br /&gt;hard to work with. Here's a more OO approach, similar to Daniel's.
&lt;/p&gt;

&lt;pre&gt;class Channel &amp;lt; Struct.new(:name, :pages)
end

class Page &amp;lt; Struct.new(:name, :items)
  def dash_count
    items.select { |i| i.dash? }.size
  end
end

class Item &amp;lt; Struct.new(:name, :type)
  def dash?
    type == &amp;quot;dash&amp;quot;
  end
end

items    = [ Item.new(&amp;quot;X&amp;quot;, &amp;quot;dash&amp;quot;), Item.new(&amp;quot;Y&amp;quot;, &amp;quot;dash&amp;quot;), Item.new(&amp;quot;Z&amp;quot;, &amp;quot;non-dash&amp;quot;) ]
pages    = [ Page.new(:Test, items) ]
channels = [ Channel.new(&amp;quot;PRO&amp;quot;, pages) ]

if logger.debug?
  channels.each do |channel|
    logger.debug &amp;quot;Pages for #{channel.name} channel:&amp;quot;

    channel.pages.each do |page|
      logger.debug &amp;quot;- *#{page.name}* with #{page.dash_count} dashboard(s)&amp;quot;
    end
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1112-can-this-be-less-clunky/refactors/374344" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor362399</id>
    <published>2009-11-16T17:15:01-08:00</published>
    <title>[PHP] On An easy one: traversing through an array</title>
    <content type="html">&lt;p&gt;I might be tempted to do something like this.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
  // $id = Request::get(&amp;quot;id&amp;quot;);
  class Request {
    public static function get($key, $default=null) {
      return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
    }
    
    public static function post() {
      // ...
    }
  }
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1101-an-easy-one-traversing-through-an-array/refactors/362399" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor359733</id>
    <published>2009-11-13T21:31:39-08:00</published>
    <title>[Ruby] On lexer</title>
    <content type="html">&lt;p&gt;Sounds like something you implement nicely in Ruby 1.9 using a lazy enumerator:
&lt;br /&gt;&lt;a href="http://www.michaelharrison.ws/weblog/?p=163" target="_blank"&gt;http://www.michaelharrison.ws/weblog/?p=163&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1098-lexer/refactors/359733" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor355403</id>
    <published>2009-11-07T00:08:38-08:00</published>
    <title>[Ruby] On JRuby : including Java classes in a module</title>
    <content type="html">&lt;p&gt;Does this not work?&lt;/p&gt;

&lt;pre&gt;module Prototype
  klasses = { 
    &amp;quot;com.utils&amp;quot;  =&amp;gt; [ 'guimodel.PresentationEntry', 'guimodel.PresentationGroup'],
    &amp;quot;com.system&amp;quot; =&amp;gt; [ 'model.ContentItem', 'model.ContentClass' ] 
  }

  klasses.each_pair { |prefix, klass| include_class(&amp;quot;#{prefix}.#{klass}&amp;quot;) }
end
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1092-jruby-including-java-classes-in-a-module/refactors/355403" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor354076</id>
    <published>2009-11-04T21:15:57-08:00</published>
    <title>[Ruby] On Fading from one color to another</title>
    <content type="html">&lt;p&gt;I think this does what you need...&lt;/p&gt;

&lt;pre&gt;def color_range(start,finish, intervals)
  range = Range.new(start[1..-1].hex, finish[1..-1].hex)
  incr  = (range.last - range.first) / intervals
  range.step(incr) { |hex| yield &amp;quot;##{hex.to_s(16)}&amp;quot; }
end

color_range('#204a87', '#729fcf', 8) { |color| puts color }
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1089-fading-from-one-color-to-another/refactors/354076" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor352829</id>
    <published>2009-11-03T03:10:58-08:00</published>
    <title>[PHP] On Determine whether it's day or night at a latlong</title>
    <content type="html">&lt;p&gt;First off, your $latitude and $longitude are not defined, so no point in including those. Seemed like most of the params you were passing to date_sunrise() and date_sunset() were or could be handled by defaulting values in the ini or via ini_set(), unless you needed them to be variable. As for the if/else logic, just say it out loud and it makes perfect sense. &amp;quot;If the current time is after the sunrise and before the sunset, then it's daytime. Otherwise, it's nightime&amp;quot;. I thought it made more sense for this function to return a boolean, to give it more reusability. Oh yeah, and using a timestamp as a return value for date_sunrise/set() makes it a lot easier to do comparisons with the current time.&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
  date_default_timezone_set(&amp;quot;GMT&amp;quot;);

  // Returns true if it is daytime, false otherwise
  function daytime() {
    $now     = time();
    $sunrise = date_sunrise($now, SUNFUNCS_RET_TIMESTAMP);
    $sunset  = date_sunset( $now, SUNFUNCS_RET_TIMESTAMP);
    return ($sunrise &amp;lt; $now &amp;amp;&amp;amp; $now &amp;lt; $sunset);
  }

  echo daytime() ? &amp;quot;day&amp;quot; : &amp;quot;night&amp;quot;;
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1086-determine-whether-it-s-day-or-night-at-a-latlong/refactors/352829" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor340237</id>
    <published>2009-10-11T20:50:45-07:00</published>
    <title>[Ruby] On Adding an object with accessor to class instance </title>
    <content type="html">&lt;p&gt;Jim Weirich's builder gem for xml is a really good example of a DSL that uses method_missing.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1061-adding-an-object-with-accessor-to-class-instance/refactors/340237" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor337050</id>
    <published>2009-10-08T06:57:34-07:00</published>
    <title>[Ruby] On Adding an object with accessor to class instance </title>
    <content type="html">&lt;p&gt;Is the only point of ObjectContainer to provide a different syntax for accessing it's elements? You could also use a Hash with your &amp;quot;MyObject&amp;quot;. &lt;/p&gt;

&lt;pre&gt;c = {}
c[:label1] = MyObject.new(&amp;quot;label1&amp;quot;, &amp;quot;value1&amp;quot;)
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1061-adding-an-object-with-accessor-to-class-instance/refactors/337050" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor337048</id>
    <published>2009-10-08T06:53:52-07:00</published>
    <title>[Ruby] On Adding an object with accessor to class instance </title>
    <content type="html">&lt;p&gt;Seems to me like you might as well just use the built-in Hash.&lt;/p&gt;

&lt;pre&gt;# Method 1 (Using ObjectContainer)
obj = MyObject.new(&amp;quot;label&amp;quot;, &amp;quot;value&amp;quot;)
container = ObjectContainer.new
container &amp;lt;&amp;lt; obj
puts &amp;quot;#{container.label}&amp;quot;

# Method 2 (using built-in Hash class)
container = {}
container[:label] = &amp;quot;value&amp;quot;
puts &amp;quot;#{container[:label]}&amp;quot;
&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1061-adding-an-object-with-accessor-to-class-instance/refactors/337048" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor335614</id>
    <published>2009-10-07T06:44:07-07:00</published>
    <title>[Ruby] On Pagination total page count</title>
    <content type="html">&lt;p&gt;A variation that's arguably less readable.&lt;/p&gt;

&lt;pre&gt;@total_pages = total.quo(per_page).ceil&lt;/pre&gt;</content>
    <author>
      <name>Ben Marini</name>
      <email>bmarini@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1060-pagination-total-page-count/refactors/335614" rel="alternate"/>
  </entry>
</feed>

