From: Brian Candler Date: 2003-07-17T02:20:25+09:00 Subject: Re: Other languages' features in Ruby On Thu, Jul 17, 2003 at 01:49:39AM +0900, Mark J. Reed wrote: > Well, Ruby *is* a compiled language; it's just not compiled all > the way to a persistent standalone executable. But since this 'compilation' is done every time you start a script, I don't think it fits with most people's definition of 'compiled'. Certainly it has to be parsed and turned into an AST, but every language is parsed - even sh. (sh parses line-by-line; Ruby parses the whole file before it starts executing; Perl does half-and-half, since a BEGIN {...} block can be parsed and executed before the rest of the source is parsed) > It's certainly > possible to do those sorts of checks in the compilation phase of > a language like Ruby; a good example is the 'use strict' pragma > in Perl, which requires that variables be declared before use. > Ruby could similarly have an option that, when enabled, would > require variables to be initialized before being referenced. It doesn't really work for Ruby though; it's too dynamic. A basic example: putz "hello world" You have no way to tell until the line is *executed* whether method self.putz exists or not - even if you could infer the class of 'self' at compile time (which you can't in the general case). Classes can be modified, modules mixed-in, or mixed-in modules modified, at any time. It worried me a great deal when I started with Ruby; it doesn't worry me now. Exercise your code in a test environment and you will catch these problems. The only problem I tend to get is in strange corner situations: if error_condition raise MyPrivateException, "debug data #{foo}" end It's only when error_condition actually occurs that the raise statement is executed, and if I mistyped 'foo' (or modified the code above which used 'foo' so it's now 'bar') then instead of MyPrivateException I will get a NameError raised. But to fix this requires declarations of all methods and variables, and the cost of doing that would be to destroy the language for a minimal benefit. Perl has a warning if a variable is used only once in a source file. However in Ruby you can't tell necessarily whether something is a variable or a method. See the 'foo' example above; if there is no 'foo = ....' before this, then foo will be treated as a method name, i.e. self.foo. It doesn't really make sense to have a warning if a *method* is seen only once in a source file: imagine, while line = gets line.chomp! puts line end would give warnings about "gets", "chomp!" and "puts" being used only once in the source file... Regards, Brian.