Skip to main content

make a change to Ruby

OK, I can't resist Pat Eyler's "Bloggin Contest"

I started programming in Ruby after having programmed in Perl for a while. There is not very much I miss about Perl, but there is one thing: in Perl, you can locate a subroutine anywhere in the script; in Ruby, any call to a method has to be located physically below the definition of the method. (Yes I know that there are reasons that Ruby's interpreter does it that way, but still...)

I write a lot of scripts that are run by non-technical non-programming users, like QA people and business analysts. The nice way to set up such scripts is to put all of the editable bits right up at the very top of the page, so that the users can ignore all of the programming guts below the important variables and such. This is easy in Perl. In Ruby, I am forced to either put all of the editable parts at the very bottom of the page, so the non-technical user has to scroll through a lot of intimidating code to get to the important stuff; or I have to distribute a separate library file and use 'require'.


Here's examples:

########################
#Perl
sub i_am_at_the_top {
print "I'm at the top\n";
}

i_am_at_the_top();
i_am_at_the_bottom();

sub i_am_at_the_bottom {
print "I'm at the bottom\n";
}
#########################
yields:
>perl -w subs.pl
I'm at the top
I'm at the bottom

#########################
#Ruby
def i_am_at_the_top
puts "I'm at the top"
end

i_am_at_the_top
i_am_at_the_bottom

def i_am_at_the_bottom
puts "I'm at the bottom"
end
#############################
yields:
>ruby defs.rb
I'm at the top
defs.rb:7: undefined local variable or method `i_am_at_the_bottom' for main:Object (NameError)
>Exit code: 1

If Ruby's interpreter would make all methods available for use at runtime, it would make communication with non-technical script users a lot more appealing.