最近一直在学习Ruby on Rails,所以顺道一起把ruby给学了。有时因为要测试语法不得不在命令行中打入:
ruby -e "comman line"
打多了后甚感麻烦,难道就不能象python的IDEL?事实上是有一个fxri的,就是太难看了。偶不要边上的大堆注解,如果它做得象python的自省那也算了,而且慢得要死。索性自己写一个好了,要求能运行单行的程序就行,但发现ruby支持多“-e”输入,所以就把多行也一起支持了。
程序是用perl写的,30几行而已。
use strict;
print "RubySgl=>";
my $cmdline="";
my $rsign="ruby";
my $esign=" -e";
my $sptr=" ' ";
my $sgl=<STDIN>;
chomp($sgl);
while(!($sgl eq "\\q")){
if($sgl eq "\\h"){
print "> > > > >execute ruby single line programme\n";
}#endif
else{
$cmdline=$rsign;
do{
$cmdline=$cmdline.$esign.$sptr.$sgl.$sptr;
print " =>";
$sgl=<STDIN>;
chomp($sgl);
}while(!($sgl eq "\\e"));
open(FOO,"-|",$cmdline);
while(<FOO>){
print "> > > > >";
chomp();
print $_."\n";
}#endwhile
close(FOO);
}#endelse
print "RubySgl=>";
$sgl=<STDIN>;
chomp($sgl);
}
然后再建立一个rubysgl.bat文件来启动:
@echo off
perl d:\rubysgl.pl
试运行,正常,8-->
redited at 2006-08-25
做了一些细小的改进,增加了行号提示和清除所有输入的功能。本来还做了几个自省函数和清除特定行的功能,但程序变得巨大无比,自己都看不下去了,实在有违初衷,因此弃用。
use strict;
my $idx=1;
print "RubySgl<".$idx.">";
my $cmdline="";
my $rsign="ruby ";
my $esign="-e";
my $sptr=" ' ";
my $sgl=<STDIN>;
chomp($sgl);
while(!($sgl eq "\\q")){
if($sgl eq "\\h"){
print " > > > > >Execute ruby single line programme.\n"; #Display IDE informatrion
}#endif
else{
$cmdline=$rsign;
do{
if($sgl =~ /^\\c\s*$/){
$cmdline=$rsign;
print " > > > > >LINES ALL CLAERED!\n"; #Clear former inputings
$idx=1;
}
else{
$cmdline=$cmdline.$esign.$sptr.$sgl.$sptr;
$idx++;
}#endelse
#continue to input
print " <".$idx.">";
$sgl=<STDIN>;
chomp($sgl);
}while(!($sgl eq "\\e") and !($sgl eq "\\q"));
if($sgl eq "\\e"){
open(FOO,"-|",$cmdline);
while(<FOO>){
print " > > > > >"; #Display outputings
chomp();
print $_."\n";
}#endwhile
close(FOO);
}#endif
elsif($sgl eq "\\q"){ exit; }
else{;}
}#endelse
print "----------another programme----------\n";
$idx=1;
print "RubySgl<".$idx.">"; #Restart programming
$sgl=<STDIN>;
chomp($sgl);
}
redited at 2006-08-26
修订了几个bug,程序改伟如下:
use strict;
my $idx=1;
print "RubySgl<".$idx.">";
my $cmdline="";
my $rsign="ruby ";
my $esign="-e";
my $sptr=" ' ";
my $sgl=<STDIN>;
chomp($sgl);
while(!($sgl eq "\\q")){
if($sgl eq "\\h"){
print " > > > > >Execute ruby single line program.\n";
}
else{
$cmdline=$rsign;
while(!($sgl eq "\\e") and !($sgl eq "\\q")){
if($sgl =~ /^\\c\s*$/){
$cmdline=$rsign;
print " > > > > >LINES ALL CLEARED!\n";
$idx=1;
}
else{
$cmdline=$cmdline.$esign.$sptr.$sgl.$sptr;
$idx++;
}
print " <".$idx.">";
$sgl=<STDIN>;
chomp($sgl);
}
if($sgl eq "\\e"){
if($idx==1){ goto RESTART; }
else{
open(FOO,"-|",$cmdline);
while(<FOO>){
print " > > > > >";
chomp();
print $_."\n";
}
close(FOO);
}
}
elsif($sgl eq "\\q"){ exit; }
else{;}
}#endelse
RESTART:
print "-----------another program-----------\n";
$idx=1;
print "RubySgl<".$idx.">";
$sgl=<STDIN>;
chomp($sgl);
}