Ruby Math with Ranges

Date published: Sun, 29 Apr 2012 15:00:00 -0700.

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.

by yksnrel on Tue, 01 May 2012 02:21:06 -0700

great link (ladder of abstraction)... looks like a shift of paradigms sparks, finally. if not known already, see http://www.kickstarter.com/pro...

by muflax on Tue, 01 May 2012 03:32:07 -0700

I'm fairly skeptical that those kind of editors will actually be of any use. The parsing sounds really expensive for anything non-trivial, good code doesn't need it, and the "look at functions" stuff is already in Emacs and other editors for like ages.

I think it would be much more useful to take a small but fast subset of those features and write addons for all major editors, so that lots of people can experiment with them and (assuming they work) the ideas can become mainstream. Even if this stuff actually works, I sure as hell won't switch to a different editor for it, and neither will any other hacker I know.

(Of course, if this stuff does work out, great! I'm not opposed to it, just not convinced it's worth the trouble or feasible with real code.)

by yksnrel on Wed, 02 May 2012 07:47:34 -0700

Yeah, the editor presented is somewhat surreal. There is no need to pretty up surfaces. I think the underlying idea can also be implemented in a textual environment. Although enhancing it visually does have some benefits. Most relatedly the 'ladder of abstraction guy' gave a great talk ('Inventing on principle') that you might have seen.