Other languages' features in Ruby

BG = Ben Giddings
JJ = John Johnson

Is there a good way to get what type checking does for you? Maybe some
catching typos, like calling the class variable @my_var, but ending up
using it as @myvar.

Test First Design?

Ok, but compare:

“Test 473 failed: expecting 17, got 18”

with

“warning: on line 765 of myfile.rb: @myvar may not have been initialized”

I think the second one is more useful, and it is a typical type
of output from compilers for compiled languages. It is much quicker and
zeroes in on the actual problem faster than a test failing. I’m of course
not advocating that Ruby become a compiled language, but it would be nice
if there were a way to do the kind of checks that a compiler does.

Well, Ruby is a compiled language; it’s just not compiled all
the way to a persistent standalone executable. 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.

Of course, as regards @my_var vs @myvar vs @myVar, the best approach
would be to pick a naming style and stick with it. :slight_smile:

-Mark

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.

···

On Thu, Jul 17, 2003 at 01:49:39AM +0900, Mark J. Reed wrote:

Not chomp! – AFAICT, the only place that ? and ! are allowed are on method
names.

-austin

···

On Thu, 17 Jul 2003 02:20:25 +0900, Brian Candler wrote:

would give warnings about “gets”, “chomp!” and “puts” being used only
once in the source file…


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada software
designer * pragmatic programmer * 2003.07.16 * 15:11:39

Oh yes, thanks for pointing that out. But I think the point stands: it is
not useful to warn on “a = foo” when foo doesn’t appear anywhere else in the
source file, because it could legitimately be a method of the object or one
of its ancestor classes. Even if there were declarations for local variables
it wouldn’t make sense:
my :foo # hypothetical declaration
puts foob # foob could still be a method

So, without introducing syntax for variables (%foo) or eliminating the
default receiver (self.puts self.foob) then I don’t think there’s a way to
give a useful warning for this case. Like I say, it used to worry me, since
Perl did catch quite a few of my typos in that way, but now I just run my
test suite and pick up the typos there…

Regards,

Brian.

···

On Thu, Jul 17, 2003 at 04:14:26AM +0900, Austin Ziegler wrote:

On Thu, 17 Jul 2003 02:20:25 +0900, Brian Candler wrote:

would give warnings about “gets”, “chomp!” and “puts” being used only
once in the source file…

Not chomp! – AFAICT, the only place that ? and ! are allowed are on method
names.