<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Rujubu's View</title>
    <link>http://www.rujubu.com/blog</link>
    <description>Thoughts on Ruby, Rails, and software development</description>
    <copyright>Copyright (c) 2007 Greg Spurrier.  All rights reserved.</copyright>
    <lastBuildDate>Sat, 02 Feb 2008 18:00:24 GMT</lastBuildDate>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/RujubusView" type="application/rss+xml" /><item>
      <title>Dynamically Creating Class and Instance Methods in Ruby</title>
      <link>http://www.rujubu.com/blog/articles/2008/2/2/dynamically-creating-class-and</link>
      <description>&lt;p&gt;Ruby&amp;#8217;s support for dynamically adding methods to existing classes is very powerful and can be used to create seemingly magic results. Unsurprisingly, Rails uses it extensively and you will also find it in the guts of many an acts_as_xxx plugin. Implementing code that adds methods on the fly can be confusing, though.  Consider this example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class ExtendMe
  def self.extend_it
    class_eval "def added_by_class_eval; end"
    instance_eval "def added_by_instance_eval; end"
  end
end

ExtendMe.extend_it
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Calling &lt;code&gt;ExtendMe.extend_it&lt;/code&gt; injects two new methods into the class in slightly different ways.  Fair enough.  But, what type of method does each end up being? &lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s find out.  First, we&amp;#8217;ll check for class methods:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; ExtendMe.respond_to? :added_by_class_eval
=&amp;gt; false
&amp;gt;&amp;gt; ExtendMe.respond_to? :added_by_instance_eval
=&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And now for instance methods:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt; em = ExtendMe.new
=&amp;gt; #&amp;lt;ExtendMe:0x35d5a4&amp;gt;
&amp;gt;&amp;gt; em.respond_to? :added_by_class_eval
=&amp;gt; true
&amp;gt;&amp;gt; em.respond_to? :added_by_instance_eval
=&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Is that what you expected?&lt;/p&gt;
&lt;p&gt;When executed in a class method, using &lt;code&gt;class_eval&lt;/code&gt; to define a new method creates an &lt;em&gt;instance&lt;/em&gt; method. Using &lt;code&gt;instance_eval&lt;/code&gt; creates a &lt;em&gt;class&lt;/em&gt; method.&lt;/p&gt;&lt;p&gt;I&amp;#8217;ll explore why this is so in a future post.  For now, just remember that it&amp;#8217;s backwards from what you might expect.&lt;/p&gt;
</description>
      <pubDate>Sat, 02 Feb 2008 18:00:24 GMT</pubDate>
      <guid>http://www.rujubu.com/blog/articles/2008/2/2/dynamically-creating-class-and</guid>
      <author>Greg Spurrier</author>
    </item>
    <item>
      <title>Color Wheel Generation</title>
      <link>http://www.rujubu.com/blog/articles/2007/12/12/color-wheel-generation</link>
      <description>&lt;p&gt;Today I was working with a set of 32 items that each needed to be displayed with a unique color.  The actual color assigned to any given item did not matter, but I wanted to make sure there was enough variation to avoid confusion among them.  I also wanted to avoid anyone saying this project was from my blue period or green period.&lt;/p&gt;

&lt;p&gt;I did a bit of web searching for color wheels and palettes containing around that many colors, but did not find anything that was immediately useful.  I found articles on the theory of color wheels, JavaScript website color scheme generators, and palettes for various desktop environments.  I just wanted a simple list of colors that were well distributed across the color space.&lt;/p&gt;
 &lt;p&gt;I ended up hacking together a quick Ruby script to generate a color wheel with 33 steps moving from red to green to blue and back to red.  After I was finished with the project I decided to clean it up a bit, generalize it to n steps, and post it, figuring that it may be of use to someone else.&lt;/p&gt;

&lt;p&gt;I am not a color scientist and I have no idea whether this is the &amp;#8220;proper&amp;#8221; way to generate a color wheel, but it worked for my purposes and I was happy with the results.  The code is below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def ramp(step, total_steps)
  (255 * Math::sin(Math::PI/2.0/total_steps * step)).round
end

# Generates the color sequence for a color wheel containting the specified
# number of positions.  If positions is not a multiple of 3, it will be
# rounded up to the next multiple.
def color_wheel(positions)
  sps = (positions/3.0).ceil # steps per segment
  colors = []
  (0...sps).each do |step|
    colors &amp;lt;&amp;lt; "#%02x%02x00" % [ramp(sps - step, sps), ramp(step, sps)]
  end
  (0...sps).each do |step|
    colors &amp;lt;&amp;lt; "#00%02x%02x" % [ramp(sps - step, sps), ramp(step, sps)]
  end
  (0...sps).each do |step|
    colors &amp;lt;&amp;lt; "#%02x00%02x" % [ramp(step, sps), ramp(sps - step, sps)]
  end
  colors
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And, here are the results when called with color_wheel(12):&lt;/p&gt;

&lt;p&gt;&lt;style type='text/css'&gt;div.color-block {width: 32px;height: 32px;margin: 1px;display: inline;float: left;}&lt;/style&gt;&lt;/p&gt;
 
&lt;p&gt;
&lt;div&gt;
&lt;div class='color-block' style='background-color:#ff0000'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#ec6200'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#b4b400'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#62ec00'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#00ff00'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#00ec62'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#00b4b4'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#0062ec'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#0000ff'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#6200ec'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#b400b4'&gt;&lt;/div&gt;
&lt;div class='color-block' style='background-color:#ec0062'&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div sytle='clear:both'&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
&lt;p&gt;Happy coloring!&lt;/p&gt;

</description>
      <pubDate>Thu, 13 Dec 2007 05:38:13 GMT</pubDate>
      <guid>http://www.rujubu.com/blog/articles/2007/12/12/color-wheel-generation</guid>
      <author>Greg Spurrier</author>
    </item>
    <item>
      <title>Rate Limiting Ruby</title>
      <link>http://www.rujubu.com/blog/articles/2007/12/8/rate-limiting-ruby</link>
      <description>&lt;p&gt;&lt;i&gt;I have decommissioned my old blog at
&lt;a href="http://www.gregspurrier.com"&gt;gregspurrier.com&lt;/a&gt;, but, from time to time, I will
re-post here some of my favorite articles from there. This article was
originally posted on September 27, 2006.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;My software is too fast! That&amp;#8217;s not a frequent lament heard from programmers.
Sometimes it&amp;#8217;s true, though, and it can be just as big of a deal as when things
are moving too slowly.&lt;/p&gt;

&lt;p&gt;I found myself in this situation a couple of months ago in my work at My Digital
Life. I was building a demo that integrated our platform with the web services
API of a popular music site. The API&amp;#8217;s terms of service required that requests
be limited to no more than one per second. Without putting safeguards in place,
our platform easily would have violated that constraint.&lt;/p&gt;
&lt;p&gt;Ensuring that a portion of code is executed at no more than a maximum rate is
not necessarily challenging, and I&amp;#8217;d done it many times in the past. This was
the first time I&amp;#8217;d tackled the problem with Ruby, though, and I was pleased with
the elegance of the solution.&lt;/p&gt;

&lt;p&gt;The code below is the RateLimiter class that I ended up with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class RateLimiter
  # Create a new rate limiter object that will ensure that actions are
  # executed with at least the specified minimum elapsed time between
  # successive invocations (specified in seconds or fractions thereof)
  def initialize(min_elapsed_time)
    @period = min_elapsed_time.to_f
    @last_fired = Time.now.to_f - @period
  end

  # Execute the given block of code.  If the time elapsed since the
  # last call is not sufficient to honor the rate limiter's configured
  # minimum interarrival period, the method will first block for 
  # an appropriate amount of time.
  #
  # The block's value is returned as the result.
  def limited(&amp;amp;block)
    # Make sure that we're obeying the limit
    now = Time.now.to_f
    if now - @last_fired &amp;lt; @period
      sleep @period - (now - @last_fired)
    end

    @last_fired = Time.now.to_f
    yield      
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Any code that must be rate-limited is simply passed as a block to the object&amp;#8217;s
&lt;code&gt;limited()&lt;/code&gt; method. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Limit to once per second
rl = RateLimiter.new(1.0)
5.times do
  rl.limited do
    puts Time.now.to_s
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;yields the result:&lt;/p&gt;

&lt;pre&gt;    Wed Sep 27 15:23:24 PDT 2006
    Wed Sep 27 15:23:25 PDT 2006
    Wed Sep 27 15:23:26 PDT 2006
    Wed Sep 27 15:23:27 PDT 2006
    Wed Sep 27 15:23:28 PDT 2006
&lt;/pre&gt;

&lt;p&gt;The same RateLimiter instance can be used to guard multiple blocks of code that
all access the same constrained resource.&lt;/p&gt;

&lt;p&gt;The beauty of this approach is that it separates the mechanics of rate limiting
from the code that is being throttled. Tomorrow I could change the RateLimiter
class to allow rapid bursts of activity while still maintaining one request per
second on average and I wouldn&amp;#8217;t have to change a line of code in the
application.&lt;/p&gt;

&lt;p&gt;Things like this are why programming in Ruby frequently makes me smile.&lt;/p&gt;
</description>
      <pubDate>Sat, 08 Dec 2007 20:04:24 GMT</pubDate>
      <guid>http://www.rujubu.com/blog/articles/2007/12/8/rate-limiting-ruby</guid>
      <author>Greg Spurrier</author>
    </item>
    <item>
      <title>Introducing Rujubu's View</title>
      <link>http://www.rujubu.com/blog/articles/2007/12/8/introducing-rujubus-view</link>
      <description>&lt;p&gt;Today I&amp;#8217;m launching a new blog called Rujubu&amp;#8217;s View.  It is about Ruby, Rails, and software development in general.  The content will be inspired both from the work I do with clients of my consulting practice, &lt;a href="http://www.rujubu.com"&gt;Rujubu Consulting&lt;/a&gt;, and from my personal projects.&lt;/p&gt;

&lt;p&gt;Welcome to the new blog and I hope you enjoy what you read.&lt;/p&gt;
</description>
      <pubDate>Sat, 08 Dec 2007 18:53:23 GMT</pubDate>
      <guid>http://www.rujubu.com/blog/articles/2007/12/8/introducing-rujubus-view</guid>
      <author>Greg Spurrier</author>
    </item>
  </channel>
</rss>
