
Dynamically Creating Class and Instance Methods in Ruby
Ruby’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:
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
Calling ExtendMe.extend_it injects two new methods into the class in slightly different ways. Fair enough. But, what type of method does each end up being?
Color Wheel Generation
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.
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.
Rate Limiting Ruby
I have decommissioned my old blog at gregspurrier.com, 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.
My software is too fast! That’s not a frequent lament heard from programmers. Sometimes it’s true, though, and it can be just as big of a deal as when things are moving too slowly.
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’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.
Introducing Rujubu's View
Today I’m launching a new blog called Rujubu’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, Rujubu Consulting, and from my personal projects.
Welcome to the new blog and I hope you enjoy what you read.
