Why no perl style 'use strict' in ruby

First let me say I much prefer developing in ruby that perl. Ive been
developing in both for about three months (maintaining old perl
scripts and doing new stuff in ruby).

I am however not totaly sure why ruby does not have something like
perls ‘use strict’. It seems that use strict is safer and can help
you spot errors qucker. I also have to confess that I prefer strong
predefined typing for the same reason but understand this may not be
as OO.

Peoples thoughts pleas,
Ben

In message
[email protected], “Ben
Edwards” writes:

I am however not totaly sure why ruby does not have something like
perls ‘use strict’. It seems that use strict is safer and can help
you spot errors qucker. I also have to confess that I prefer strong
predefined typing for the same reason but understand this may not be
as OO.

Ruby’s defaults are in some cases closer to “use strict” than perl’s;
for
instance, you can’t accidentally create a global variable when you meant
to create a local variable. In other cases, the looseness is a central
theme of the language’s design.

Duck typing is a philosophical choice; an option to remove it would be
ridiculous. It’s like asking for a C variant where you can’t address
memory using pointers.

-s

On 7/13/07, Peter S. [email protected] wrote:

Duck typing is a philosophical choice; an option to remove it would be
ridiculous. It’s like asking for a C variant where you can’t address
memory using pointers.

Pointer-less C:

http://www.embedded.com/showArticle.jhtml?articleID=192503623

:slight_smile:

I am still looking for a better answer to this one.
Problem is, when I have the equivalent (erroneous) code in both, perl
and ruby I can run perl/ruby -c to do a syntax check, but only perl will
throw an error about the mistyped/undefined $y

#!/usr/bin/perl -w
use strict;
sub foo() {
my $x=5;
print $y;
}

foo();

#!/usr/bin/ruby -w
def foo
x=5
puts y
end

foo

On Jul 13, 2007, at 8:54 AM, Ben E. wrote:

I am however not totaly sure why ruby does not have something like
perls ‘use strict’.

The strict pragma does three things in Perl. Two of the items are to
forbid the use of symbolic references and “barewords.” Ruby doesn’t
support these features, so it’s not an issue.

The other feature of the strict pragma is to avoid creating random
global variables every time one is mentioned. In Ruby, globals look
different from other variables (the leading $), so this is not really
a problem. Local variables need to be assigned to before use, since
that’s Ruby’s method of declaration. That solves the same problem
the strict pragma handles for Perl.

James Edward G. II

Bernhard M. Wiedemann wrote in post #1176676:

#!/usr/bin/ruby -w
def foo
x=5
puts y
end

Perl variables can be immediately told apart from method calls, it is
easy for the syntax checker to pinpoint an undefied variable, but in
Ruby, method calls and variables look the same.
If you refer to an undefined variable, Ruby throws this error:

puts y
NameError: undefined local variable or method `y’ for main:Object

In Ruby you can define a method using metaprogramming:

define_method(“y”) do |arg|
puts arg
end

Which is too much for a syntax checker to understand.
In the end the syntax checker sees you used “y” but it does not know
whether it has been defined by some metaprogramming wizardry or not.

You are looking for static analysis tool, right?
Try ruby-lint!

 ruby-lint test.rb
test.rb: warning: line 2, column 3: unused local variable x
test.rb: error: line 3, column 8: undefined method y