le_me le_me - 7 months ago 15
Ruby Question

What does a macro definition with the same token twice in C mean?

I was reading the Ruby source code when I stumbled upon the following:

#define RSTRING_EMBED_LEN_MAX RSTRING_EMBED_LEN_MAX


context

As far as I can tell
RSTRING_EMBED_LEN_MAX
is not defined before. (I grepped the Ruby source recursively).

What does that line do exactly?

Answer

This define will expand into the literal token RSTRING_EMBED_LEN_MAX, just as you would expect.

By looking at the source, you'll see, that the macro is used to define an enum value with the same name, set to (int)((sizeof(VALUE)*3)/sizeof(char)-1).

The commit, linked by matt, explains they converted all macros into enums, to improve debugging. And the macro was left for compatibility reasons.