Share code/snippet that you are proud of writing

If you have an Open Source project, StackOverflow answer, gist of a code snippet, or any other post/comment with code that you feel proud of having written, please share it here. Also please share why you’re proud about it. Code snippets in the post are welcome.

As a general rule people please refrain from correcting or improving upon people’s contributions to this post unless they specifically ask for it. We have people coding at all levels of experience and any new discovery can be a proud moment of code. So please keep in mind peoples feelings as they share in the pride of their code.

Now if you don’t mind “helpful” critiques then be sure to ask for it when you share your code.

For me the code I’m most proud of is PolyBelongsTo. It’s a power tool for ActiveRecord for all the things you wish you had (if not please suggest a feature). What I’m most proud about with it is my use of self-recursive code used for ActiveRecord recursive duplication and finding the top parent record. Self-recursive code is a concept I’ve learned and somewhat mastered recently. Everything in this gem is fully documented and tested.

As for critique of my code please do so on the PolyBelongsTo github issues section. I welcome any issue submissions.


Nice one Dan :+1:

I remember you saying how popular the gem was becoming - it’s one of those ‘why isn’t it in Rails’ gems!

With regards to my proudest code it’s actually my first app - everything from all my books was fresh in my head and I was taking my time with things, my other two apps were put together in days and it shows haha (really lazy coding).

With regards to a specific moment… my first real bit of metaprogramming - where I replaced a few hundred lines of code with just a handful :smiley: (nothing really all that impressive but back then it made me happy lol)

1 Like

There was a bug in ActiveSupport where upcasing a string with the Danish letter Æ, Ø or Å prior to a special library being loaded, would return some bogus string. Something like


would return “BARROW” or “BENCH” or whatever, instead of the korrekt “BEMÆRK”.

We had to implement this hack in our backend, in order to allow upcasing Danish words:

class ActiveSupport::Multibyte::Chars
  alias :_up :upcase
  def upcase
    _up rescue nil # There be dragons

I use this as my alarm clock regularly:

#!/usr/bin/env ruby
sleep(eval(ARGV.join)) # example `alarm "8*60*60+5*60"` for 8 hours and 5 minutes
sound_bite = Dir.glob("/Motivational Speech/Start The Day Right/*.mp3").sample
%x^/usr/bin/env mpg123 "#{ sound_bite }"^
1 Like

New gem! Demisyn (as in Demi-Syntax)

Imagine if you could write your ruby code in English without dots between method calls. Well this is a step towards that reality. It’s a bit crazy, and that’s why I like it. :smile:

I’m just trying to imagine use cases with, ~HEREDOC … so many different ways to absorb ordered English words and run it as code.

In case you haven’t looked yet, the gem lets you do this:

~"'asdf' reverse split('s') join capitalize"
# => Fda

Currently used as a refinement for use in Objects. If there’s interest I could just make it available everywhere. But I think scope is a very good thing.

1 Like

I’d like to see more people posting code here. Here’s the code for my most recent gem env_compat. I’m still putting new things I learn to use. I’d love it if you all took the time to look at this and give me your perspective or questions on the code.

My use of my CodeBlock object was me thinking of thread safety and this is my first time using Kernel.private_constant. I really enjoyed writing this :smile:

1 Like

Here’s one @Ohm has contributed to that I’m proud of. clock_window - it’s both a script and a library. Clock Window is a productivity tool that will record your window activity for you and how long each window was active. There’s a lot of things I did in this code that I’ve never done before and it’s pretty powerful! @Ohm brought in the Mac support so it’s compatible with both Linux and Mac! :smile:

Some of the crazy things I tried was an infinite loop script designed for manual breakage to write the data out with. Some regex string stripping filters through both gsub and match. Automatic directory creation with date integration for directories/files. And Slop for command line arguments. All in all it’s been really fun to work on.

1 Like

The code in order_query that builds nested conditions for keyset pagination. Started off with ugly loops and string concatenation, then noticed the entire thing can be expressed with several foldrs and refactored into half the original size.


Those are the best refactorings!

1 Like

I’m not sure if it’s in the spirit of the original post, but I enjoyed writing this recently — a Tweet-sized one-liner for counting how many words Hamlet speaks in the eponymous play (warning: contains unashamed use of the flip flop operator):

curl -s '' | ruby -lne 'print if /^Ham\.$/ .. /^(?!Ham)[\w ]+\.$/' | ruby -ne 'print if !/^\[.+\]$/ && !/^[\w ]+\.$/' | wc -w

Leave off the wc and you’ll get only Hamlet’s lines; switch out the “Ham” for someone else (e.g. Pol for Polonius) and you’ll get their lines.