Oh my god! This one pass Ruby parser is really annoying. Here’s todays example that doesn’t compile:
DICTIONARY = create_dictionary def MyClass.create_dictionary ... end
Yep, you got it, this blows up with a nice error message stating that the variable or method named create_dictionary couldn’t be found. Move the constant definition below, everything is fine. Lame.
Well Ruby is an object scripting language. The emphasis is on scripting. Hence also the lame require/load and lack of decent module namespacing. Matz just needs to take some pointers from guido/joy on this one.
LikeLike
Lots of scripting languages are read then loaded in line… this is what is happening here, the interpreter doesn’t have reference to the method you are defining until the ‘end’ tag so trying to define something in that space will always blow up… (unless someone wants to have a multi-pass compiler which are a bear.)
LikeLike
Yeah, you are completely correct and it gets much, much worse! I’ll be posting again about this Ruby parsing stuff, but reflection in Ruby is such a mess because it doesn’t build up an AST completely.
LikeLike
It’s very simple: class and method definition are run-time operations, not compile-time. Ruby does parse the whole file, as you can see if you put some real syntax errors later in the file (like a = + ] ).
Having class and method definition run-time means you can also redefine methods (and even undefine classes) at run-time. This means that ‘fixing’ your problem would be very hard: If there are two definitions for create_dictionary, which one should Ruby use at that point in the code?
LikeLike