Monday, 7 July 2014

Ruby Custom String Delimiters

So, as I'm in the process of transitioning to Bitbucket, which will eventually entail a proper website-ish thing, I'll actually be posting some more developer style stuff here.

First subject: Ruby Custom String Delimiters. This is a feature of Ruby which just shouldn't be allowed. Basically, Ruby claims it can substitute an arbitrary symbol for a string delimiter, by prefixing the delimiter with %q or %Q (depending on if you want a double or single quoted string, which are different in Ruby). Also using a symbol which has a matching closing symbol will cause the closing symbol to be the end of the string (e.g. using [ causes ] to end the string).

Got that? Well, here's some counter-intuitive stuff you have to deal with when trying to parse the damn thing, when playing around with IRB and Ruby 1.8:
  •  %q"Hello\n" == "Hello\n" => False (so it's insufficient to look at quotation mark to determine type of string)
  • %q][] does not end the string
  • %q][]] is a syntax error
  • %q£Hello£ quits IRB  
I just can't conceive of a circumstance where this feature is actually useful, outside of obfuscated code competitions. I've programmed in a fair few languages, and I've never once thought this would be useful enough that it outweighs making the code harder to read. Even if it's inherited  from another language, this is a good example of why I don't like Ruby: too much flexibility in the language can be counterproductive to understanding the language.

2 comments:

  1. Handy if you're using lots of strings full of single and double quotes. It could make your code easier to read than strings loaded with backslashes. Small consolation when you're trying to parse them though.

    Perhaps you should ignore lesser-used features like this for now and just get a version out that handles the standard use cases?

    Also, as with anything useful in an obfuscated code competition, you can blame perl for this feature.

    ReplyDelete
    Replies
    1. Truth be told, it's not that hard to implement. The code I've to parse is guaranteed to be valid, so a lot of the problems don't matter. I'm just questioning why this feature is in the language at all.

      If you're using lots of strings full of single/double quotes, then it'd be much more sensible to use a Heredoc-style string. Python also has a good solution, in that you can use single or double quotes interchangeably, and it's unlikely you'll be using both heavily. Add in the triple single/double quote syntax, and you just get the idea that this custom string delimiters doesn't really have any value.

      And unfortunately, not implementing it isn't an option. This appears in 2 of the games I've got as my test bench, so it is probably common enough. As an aside, I'd also argue that using #{ } to embed code containing string into a string is a daft thing to do, but that also crops up. (I've even got a "#{"Hello"}" type thing somewhere, which is just bizarre.

      Delete

Note: only a member of this blog may post a comment.