Last modified: 2012-04-30 (finished). Epistemic state: believed.

You’re playing some D&D game, like you always do, and you find two weapons, the Sword of Burnination, 1d8+3 and 1d6 fire damage, and the Axe of Castigation, 2d6+3. And you’re tired of doing this math in your head.

So you write a Ruby gem to do it for you.

First, you write a simple gem to do math with ranges. You wanted this since you read Up and Down the Ladder of Abstraction. Now this works:

(1..6) * 3      # => (3..18)
(-5..3) + 7     # => (2..10)
(1..3) + (2..4) # => (3..7)
(4..12) / 2     # => (2.0..6.0)

You can do quick math in your irb sessions with ranges instead of numbers now. You’re happy.

You do a gem install range_math on any system, and put this in your .zshrc:

alias i="irb -rrange_math"

Then you write a simple script to convert dice rolls into ranges, put it in your PATH and alias it like this:

alias avg="noglob average_damage.rb"

Now this works:

$ avg 1d8+3 + 1d6
range: 5..17, average: 11.0

Suffering ends.

blog comments powered by Disqus
blog » omg hax » ruby math with ranges