Category Archives: Ruby

On Module Integrity

A reply to Josh Cheek’s post “Modules called, they want their integrity back”. Continue reading

Posted in Ruby | Tagged | 2 Comments

Ruby Thread Locals are also Fiber-Local

I was briefly concerned that thread-local variables would not also be Fiber-local, since fibers have their own stack. This would be a problem for any code which uses thread-local variables to delimit a stack context, e.g. to implement dynamically-scoped variables … Continue reading

Posted in Ruby | Tagged , , , | 4 Comments

Preventing Recursion in Ruby

A new post on the CodeBenders blog about how to prevent a method from accidentally falling into an infinite recursion. Continue reading

Posted in Ruby | Tagged , , , | Leave a comment

Decoration is best, except when it isn’t

I think by now we all know to prefer composition over inheritance. But in a language with a lot of options, what’s the best kind of composition to use? Composing an adventure Consider an adventure game, with objects representing player … Continue reading

Posted in Ruby | Tagged , , | 9 Comments

Testing that a block is called

CapnKernul asks: Hey Avdi. How would you test that a method’s provided block is called in RSpec? Would you stub #to_proc (for &block) and mock #call? Typically the way I test that a block is called goes something like this: … Continue reading

Posted in Ruby | Tagged , , | 4 Comments

Temporarily disabling warnings in Ruby

I’ve been doing a lot of pontification lately. Time for some code. Warnings are a hot topic in the Ruby community. Some folks love ‘em. Some folks hate ‘em. What we can all agree on is that they do us … Continue reading

Posted in Ruby | Tagged , , , | 6 Comments

Your Code is My Hell

It occurred to me recently that my experience as a Rails developer may be somewhat unique. I often get brought in to help preexisting Ruby/Rails projects evolve and mature in a sustainable way. As a result, the vast majority of … Continue reading

Posted in Rails, Rants, Ruby | Tagged , , , , , | 121 Comments

Do we need constants?

This article by Joey Butler about constants in Ruby got me thinking. How much do we really need constants, anyway? As Joey points out, constants are an opportunity for implementation details to leak out into other classes. But they complicate … Continue reading

Posted in Ruby | Tagged , | 29 Comments

You Can’t Subclass Integers in Ruby

This post is mainly just an excuse to test a Gist plugin for WordPress. Occasionally, you might think it would be handy to subclass Numeric types such as Integer. For instance, you might want to create a constrained integer which … Continue reading

Posted in Ruby | Tagged , , , , , | 8 Comments

Loading plugins with Rubygems

Let’s say you have a Rubygem named “blorf”. You want to enable other developers to write plugins in the forms of Rubygems of their own. For the end user, loading the plugins should be as simple as writing: require ‘blorf’ … Continue reading

Posted in Ruby | Tagged , , | 11 Comments