Variable scope in Ruby
One big difference between Perl and Ruby is that Ruby has no lexical variables. This confuses me when programming Ruby, because I’m used to thinking ifwill create a new scope. It doesn’t. Variables defined inside if will be available outside, because they belong to the method scope. For example, in Ruby you can do this: def foo if true x = 2 end puts x end foo This prints 2. However, in Perl: sub foo { if (1) { my $x = 2; } print $x; } foo(); You get the warning Use of uninitialized value $x in print at lexical_test.pl line 5.. ...