1. 14
  1.  

  2. 2

    The author goes out of the way to use confusing examples (like the nested here-doc example). I guess the point is that it’s possible and thus a feature. More likely it seems like this is party to Ruby’s conservative evolution, choosing to add rather than break old code.

    I can think of many more surprises, like the validity of def inside nonobvious contexts like argument default expressions, the BEGIN and END block feature and how nesting BEGIN and END inside BEGIN or END can be mind bending.

    It’s interesting to consider how hard it is to specify behavior after the fact, Ruby primarily being determined by it’s primary implementation in C (MRI/YARV/whatever it’s called these days). There were some removals going from 1.8 to 1.9 and 2.0 but most of the features remained intact, which is an impressive but scary proposition for such a complicated language. The fact that RSpec even manages to check such a wide set of edge cases is an impressive bit of work (I recall when the initiative was started and it took years to even get a baseline coverage for behaviors used in common things like Rails).

    If you’re not working on designing the language, the other takeaway might be to consider what libraries you use in Ruby very carefully to avoid things that tread too close to being unexplainable or risky. Of course, it’s hard to avoid many of these “magic tricks” as popular frameworks and libraries tend to operate on them by selling the idea that the DSL is more important than the boundary between comprehension and code you write. Rails seems to have cleaned up some of its more offensive practices but it’ll be interesting to see if the long term use of Ruby will remain closely tied to the preference for magic over boring old Ruby code which avoids all of these knobs.

    I’ll admit to having written some very experimental Ruby code in the past, though as I maintained more code over the years, I started gravitating towards simple and obvious code when possible. Now I haven’t written Ruby recently (none for many years now) but it’s interesting to see people get polarized over the malleable and/or sharp bits of Ruby. It’ll always be part of Ruby even if you don’t use it.

    1. 2

      Nested heredocs

      p <<"A#{b}C"
      #{
        <<"A#{b}C"
      A#{b}C
      }
      str
      A#{b}C
      

      Nuff said, I’m quite sure that there are no syntax highlighters that can handle this code.

      Coderay handles it properly: http://coderay.rubychan.de/rays/9186