1. 1 Nit Khair

    Could you pls explain how the 3rd is clearer or more elegant than the first.

    I am easily able to follow the first, I am a tad lost with the other 2. Would it not take most people more time to figure out … I am a newbie, forgive me if its easy for most rubyists.

    Thanks.

  2. 2 Matt Williams

    I’m thinking that inject is clearer in some ways because it isn’t needing an extra variable to be declared outside of the loop and then explicitly returned.

    The 2nd method certainly is kludgy, I think. Getting at the contents of the string takes some work. Of the 2nd and third methods, I prefer the third — for one thing, there’s a lot less method chaining going on.

    Part of the point, I think was that there’s more than one way to write the code and some may be more efficient and/or elegant than others. The first version certainly works, I just feel that having to declare variables outside of the loop and return it explicitly seems awkward. Of course, the each_byte method is pretty nice, but I don’t know that it makes up for the extra work. It’s far too easy to forget to return the value of the external string, methinks.

Leave a Reply