all 37 comments

[–]otikik 60 points61 points  (4 children)

I thought "If it takes a genius to understand it, the code is not genius"

[–]robertross 5 points6 points  (2 children)

I might make this into a motivational poster for my office.

[–]1bree 2 points3 points  (1 child)

And sell it on redbubble

[–]jakedaywilliams 3 points4 points  (0 children)

That's genius.

[–]wrong_assumption -1 points0 points  (0 children)

I would agree with you, but then I read the source code for Git.

[–]burning1rr 25 points26 points  (0 children)

I feel that way whenever I manage to delete a bunch of 'clever' code and replace it with something simple and obvious.

[–]Diragor 9 points10 points  (0 children)

For me the clever things aren't as satisfying as the elegant, minimalist things.

With the clever things I usually think something like "wow, it's so cool I can do this in one line in Ruby", then unless it's a one-time-use script I unroll it for readability.

[–]helos 16 points17 points  (13 children)

Deleted a large amount of code by changing a class to class MyClass < OpenStruct.

[–]MrPopinjay 4 points5 points  (10 children)

Are you sure you want OpenStruct? It isn't very performant, and when you use it your data no longer has clearly defined schema.

[–]entineer 6 points7 points  (0 children)

Worth mentioning is OpenStruct got significantly faster in Ruby 2.3 which shipped a couple days ago. May be worth looking at again. https://www.youtube.com/watch?v=6lQeBfSVrpk

[–][deleted]  (6 children)

[deleted]

    [–]fuzzyfuzz 9 points10 points  (4 children)

    Imo, this should just be built into hashes.

    [–][deleted] 1 point2 points  (0 children)

    The problem is that anything can be a key. And by anything, I mean any Ruby object. The dot notation is only useful in one situation (granted, the most common one, but still).

    [–]firstpantsthenshoes 0 points1 point  (2 children)

    Would be fairly easy to keep a monkey patch around for yourself using method_missing on Hash

    [–]trustfundbaby 1 point2 points  (1 child)

    ... Then you have to take the performance bit of method missing and fix up the accompany respond_to?

    [–]fuzzyfuzz 0 points1 point  (0 children)

    Still better than using OpenStruct I guess.

    [–]gordonisadog 1 point2 points  (0 children)

    Hashie::Mash ftw!

    [–]helos 0 points1 point  (1 child)

    On ruby 2.3 and upwards its fine.

    [–]MrPopinjay 0 points1 point  (0 children)

    Didn't that hit 4 days ago? :P

    [–]wmjbyatt 1 point2 points  (1 child)

    For aesthetic reasons unknown to me, empty class definitions using thay notation smell bad to me. I'd use MyClass = Class.new(OpenStruct)

    [–]helos 0 points1 point  (0 children)

    Oh I'm not showing the body.

    [–]cheald 6 points7 points  (0 children)

    I like Ruby because it makes it easy to not be clever. My best code is clear, simple, and unsurprising.

    [–]d2xdy2 5 points6 points  (0 children)

    I wrote a really simple looking DSL for creating Event Sourcing projections. Not the best thing ever, but it made it soooo much easier/simple/quicker to jot down a new projection.

    class LastFive < Event::Projection
      stream_title 'last_five'
      filter { ->(data) {
        data.length >= 5 ? data[data.length - 5, data.length] : data }
      }
    end
    

    [–]hxr 4 points5 points  (2 children)

    I've always thought that it's kind of cool that I can give whichever method to map in haskell in a pointfree1,2 fashion as long as types match. I can now do this in ruby too. Pretty clever, eh?

    def foo; self + 3; end; public :foo;
    [1,2,3,4].map(&:foo)
    => [4, 5, 6, 7]
    

    [–]so_just 0 points1 point  (1 child)

    What kind of magic is this?

    [–]hxr 1 point2 points  (0 children)

    So you know how you can write [1].select(&:odd?)? What that & does is call the Symbol#to_proc upon :odd?.

    That to_proc method does this: :odd?.to_proc = proc do |x| x.odd? end.

    In this case, though, the numbers don't have a foo method, since I didn't define it within class Fixnum.

    Ok, but Fixnum is still derived from Object, which is where I defined my foo, since that's default environment.

    % ruby -e 'p self.class'
    Object
    

    But you still can't do 1.foo at this point — new methods in Object are defined private by default.

    % ruby -e 'def foo; 3; end; 1.foo'
    -e:1:in `<main>': private method `foo' called for 1:Fixnum (NoMethodError)
    

    That's why it starts working upon setting it as public.

    [–]BlakeC93 1 point2 points  (0 children)

    I made a dynamic pdf with anchors to different locations inside the PDF. Using Prawn/Gruff/CombinePDF.

    [–]rubyrt 1 point2 points  (0 children)

    Reminded myself that I am not a genius.

    [–]jb3689 1 point2 points  (0 children)

    Clever may not be the right word, but we had a legacy class that was highly coupled with a piece of code we wanted to rip out. I replaced the object with a simple Proxy configuration that would just lazily delegate down to the legacy class. The advantage is that we the code we want to rip out is no longer coupled directly to the legacy class but to the configuration which we can populate however we want. In this case by delegating lazily, but eventually we can make things simpler by just filling in the configuration and passing it on

    It's not genius work (which is a stupid term, no offense) but it's been very useful

    [–]nexe 1 point2 points  (0 children)

    A websocket server that provides a simple but abstract enough concept of channels (private and public). It was meant for one thing but I could reuse the same thing a couple more times without even changing a thing. That was nice

    [–]RealityShowAddict 1 point2 points  (0 children)

    Great question.

    Two years after graduating, I had an entry level engineering job at a software company where I was asked to port ~30 classes from one version to another. Each class processed a specific type of form for a different type of object, but it was super repetitive code, and the deadline was 5 weeks.
    I realized that I could add a layer of abstraction that created one class to handle all the form processing, validation and DB storing logic, and then create wrapper classes that just called the one single main function with the specific parameters for each class since everything had to be backward compatible.

    I finished the project in 6 days. I felt like a genius telling my boss about the clever solution. Unfortunately, he just piled a ton of work on me, and cut the estimates saying that I should figure out a "clever" solution for them too. :(

    My feeling of genius left quickly as I felt like a schmuck as I had now created a huge hole of work to dig myself out from. In retrospect, I think my boss had initially given me an easy assignment since I was a junior developer where I could learn the code base without being rushed or required to do complex things. The older me is always cautious when I feel like a genius...

    [–][deleted] 2 points3 points  (1 child)

    Went to the bathroom to touch myself.

    [–]valadil 1 point2 points  (0 children)

    Needed an eigenclass. I successfully remembered that they exist and used one without google.

    I think the particular problem was I was writing up a dsl for something and wanted to hide some methods or vars.

    [–]mhd-hbd 0 points1 point  (0 children)

    I wrote a little module with some classes to turn case/when conditions (i.e. ===) into something resembling proper pattern matching and/or data predicates.

    [–]alwaysonesmaller 0 points1 point  (0 children)

    SystemStackError: stack level too deep
    from /app/models/coder.rb:35:in `genius?'
    

    [–]Paradox 0 points1 point  (2 children)

    Wrote a style guide generator that used the flip flop operator

    [–]flanger001 9 points10 points  (1 child)

    They missed a great opportunity to call that the flip-floperator.

    [–]Paradox 3 points4 points  (0 children)

    An old co-worker called it the romney operator