The Creative Programmer strikes again



  • Now that The Creative Programmer™ (Ruby On Rails Edition) mastered the Time, he moved to a bigger challenge. And that would be removing a first character from a string. Someone might think, no big deal, just do some_string.slice!(0) and we're done. Not The Creative Programmer™ though. He takes a closer look at Ruby's String API and finds two exciting methods : "chop" (which cuts the last character) and "reverse" (which actually reverses the string). So the final solution is (as you already guessed) :

       some_string.reverse.chop.reverse



  • Oh. My. Dear. Lord!

    Wait, if he was perusing docs that were alphabetically sorted, he saw chop and then reverse before he would have found slice - any chance he took the first combination he found that seemed to accomplish his task? Still, common sense should have told him there was a better way.

    Please, please, please keep these coming!

     



  • When common sense just doesn't work for you...

    Was this guy fresh out of college? 



  • Hm.. sounds a lot like one of my colleagues back in college. He'd do some weird stuff to get the job done ... in fact, thanks to him I've seen incredibly unique stuff... like a non-LIFO stack, and a non-FIFO queue. The things he made inspired fear on the hearts of those who had to check his code. Modifying it? Oh no ... he's GOD, and nobody can touch his sacred code. Good thing he was mainly a VB6 developer, so I wouldn't even dare to touch most of his stuff, but I did one or two projects where he did stuff in C++, so we had to make our code interoperable. I still have nightmares to this day ...



  • @DOA said:

    When common sense just doesn't work for you...

    Was this guy fresh out of college? 

     

    Never got a chance to meet this guy. Not that I'm really into it...



  • i've never used Ruby on Rails, but I must admit I find rather amusing that they have function names like "chop" and "slice". Do they have "puree" instead of "random" as well?



  • @campkev said:

    i've never used Ruby on Rails, but I must admit I find rather amusing that they have function names like "chop" and "slice". Do they have "puree" instead of "random" as well?
    Don't know about RoR, but GNU C has a function called strfry() to create an anagram of a given string.



  •  Ruby also has chomp, which removes the trailing newline from string input, and squeeze, which removes whitespace (analogous to Trim())



  •  Who/what's the creative programmer?



  • I've come up with worse when I was learning to program... My first language was C++ and when I was learning about data structures I came up with a randomly assessed linked list! The solution was simple... Allocate the nodes on a contiguous chunk of memory, and to random access just do

    return ((Node*)&this->m_data[index * sizeof(Node)])->Data;

    Want to create an element? Simple:

    // this is more-or-less psudo code I just typed up. I no longer have the old code, but this is the basic method I used:

    int size = (this->m_count + 1);

    Node* temp = new Node[size];

    memcpy(temp, this->m_data, sizeof(Node) * size);

    delete [] this->m_data;

    // alot of code to re-fix all of the m_next/m_prev pointers...

    temp[size].Data = new_data;

    this->m_data = temp;

    // done!

    I was really clever back then... You get the benifiet of random access, yet with the cost of all of the drawbacks of a vector without the benefits of a list...

    So you can't really blame him... But all the same, I love laughing at others' silly mistakes almost as much as I love laughing at my own :)



  • i've never used Ruby on Rails, but I must admit I find rather amusing that they have function names like "chop" and "slice". Do they have "puree" instead of "random" as well?

    FYI, chop is part of the Ruby core, not the framework/lib Rails.


Log in to reply