Tag Archives: ruby

Generating cows with IO.popen()

I find the subject of starting and interacting with other OS processes fascinating. A few years ago I wrote a never-completed series on the many ways to spawn off processes in Ruby: Part 1: Backticks and system() Part 2: Opening … Continue reading

Posted in Ruby | Tagged , | 2 Comments

Unless readable else confused

An exploration of why unless…else in Ruby is problematic at best. Continue reading

Posted in Ruby | Tagged | 10 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 , , , | 1 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 , , | 10 Comments

Another Take on Smalltalk-Style Controll Flow (SBPP #6)

The latest Smalltalk-to-Ruby translation in my SBPPRB archive is “Dispatched Interpretation”. It’s one of the bigger ones I’ve tackled so far. I’m not going to go over the whole pattern here; for that you’ll just need to buy a copy … Continue reading

Posted in Smalltalk Best Practice Patterns | Tagged , , , | 2 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 , , | 6 Comments

Defining #method_missing and #respond_to? at the same time

I was reading Eloquent Ruby yesterday morning (buy a copy if you haven’t already), and it got me thinking about one of my “favorite” Ruby gotchas: defining #method_missing without a corresponding #respond_to?. E.g.: class Liar def method_missing(*args) “Oops, I lied” … Continue reading

Posted in Stupid Ruby Tricks | Tagged , , , | 24 Comments

DRYing up your validations using DB reflection

Avoiding silent truncation of your model fields requires putting length validations on them. But this can introduce duplication of knowledge. In this post I demonstrate how to pull limit information directly from the DB into your model validations. Continue reading

Posted in Rails | Tagged , , , | 26 Comments

RVM.el and Inf-Ruby (Emacs Reboot #14)

With my Emacs config files better organized, it’s time now to turn my attention to improving my experience editing Ruby files. First of all, I want to be able to quickly and easily drop into an IRB session from the … Continue reading

Posted in Emacs Reboot | Tagged , , | 4 Comments