Commenting out blocks of ruby in emacs

I’ve been an emacs user for 12+ years, and am ashamed to admit that I’ve never learned emacs lisp (yes, my last post was about emacs lisp code, but it was a copy and paste with a few changes, and I certainly didn’t understand what it was doing). Until now, that is. I started reading the emacs lisp intro, but only got about fifty pages in to it before I got the itch to write some code.

So here it is. Its a package that allows you to comment out a block of ruby code from a keybinding (I’m sure there is code out there to do this already, but it was a fun learning challenge).

The main function is comment-ruby, which I have bound to C-:. If there is a region active, it comments out the region. If that region is less than five lines, each line is commented with ‘# ‘, otherwise the entire region is wrapped in =begin...=end. If the region is not active, the current line is commented out with ‘# ‘.

After a region has been commented out, the point (cursor) is set to the line above or below the region, depending on whether the point was at the beginning or end of the region when called.

Here is the code – it probably isn’t idiomatic lisp. If you have any suggestions on improving the code or making it more idiomatic, please let me know!