Tag Archives: ruby

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 , , , | 3 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

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 , , | 4 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 , , , | 25 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 , , | 3 Comments

Declarative Keyword Parameters in Ruby (SBPP #5)

Reading SBPP has got me thinking about keyword parameters. Ruby doesn’t have keyword arguments, but it fakes them pretty well: def explain(options={}) “the #{options[:the]} says #{options[:says]}” end explain the: “pig”, says: “oink” # => “the pig says oink” explain the: … Continue reading

Posted in Smalltalk Best Practice Patterns | Tagged , , | 8 Comments

SBPP #4: method Cascades

In Smalltalk, you can “cascade” side-effectful calls to the same object using the semicolon (;) operator. E.g.: If I understand it correctly, the semicolon is effectively a K-combinator or “Kestrel”. I am jealous. Sure, we have Object#tap, but that’s awfully … Continue reading

Posted in Smalltalk Best Practice Patterns | Tagged , , , , | 7 Comments