1

Before you mark this as a duplicate of "What is the colon operator in Ruby?", read the question (just being preemptive). I'm not asking about what the colon actually does.

I was recently trying to come up with a clever solution to "Convert string to symbol/keyword". My first thought was to overload the colon operator for the String class, just like Ruby does for the + method on String (I'm aware of the potential downsides of doing that). But I discovered that the : is not simply an overloadable operator of String.

The first result on Google for "ruby operators" does not show the colon as an operator. And I can't find any sources that claim it is an operator. I suppose it would fall under the same category as the hashrocket (=>) but I don't know what you would call these.

So what is this special colon character classified as? And can you point to its definition in the Ruby source where it can potentially be altered?

Community
  • 1
  • 1
Mike S
  • 11,329
  • 6
  • 41
  • 76
  • "where it can potentially be altered?" is a red-flag. Even when possible, basic behaviors of a language should not be changed as it makes the language behave in unexpected ways, make it break working scripts, and can make code become unmaintainable without special "tribal" knowledge. It's better to go to the maintainers and work with them to learn the ins and outs and discuss why such a change should be made and, more importantly, why not. – the Tin Man Aug 20 '15 at 20:12
  • @theTinMan I think that depends on where you draw the line for a "basic behavior of a language." For example, Monkey Patching could be considered altering basic behavior depending on the scope of the change with respect to a given code base, but when used correctly/narrowly has tremendous benefits. But I suspect the maintainers have good reasons that **operators are overloadable** and **keywords are not**. They might define "basic behavior" as something hardcoded in the parser, whereas anything changeable via class extension is not. – Mike S Aug 20 '15 at 21:54
  • But I agree :) altering the behavior of `:` would do much more harm than good. – Mike S Aug 20 '15 at 21:55
  • C++ allows overloading operators left and right, and, early on, people went overload-happy because they had "better ideas". It resulted in code that made no sense when read. Over time people learned that doing so leads to madness, their own, and severe irritability in those who had to work with the code later. It was a software-industry maturing process. Defining a language's syntax is an art in itself. – the Tin Man Aug 20 '15 at 22:14

1 Answers1

5

Parsing a program takes place in two steps:

  1. turning the sequence of characters into sequence of tokens, and
  2. turning the sequence of tokens into a syntactic tree.

Operators, simple object literals, keywords (such as begin), => are all tokens, and are handled in step 2. Tokens have internal names in Ruby C code.

However, the colon as part of a named parameter in a method or a symbol key in a hash is handled in step 1, and is not a token; it does not have a particular name.

sawa
  • 165,429
  • 45
  • 277
  • 381