[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Difference between using irb and window promt

Li Chen

11/26/2006 9:29:00 PM

Hi all,

I just write a script to change the filenames within a folder. Out of
curiosity I want to see the differences bewteen using irb and window
promt. I run the script from the window promt and I get what I want. But
when I paste all the codes to irb and run from there I get error
messages. In theory there is no defference using either irb or running
from window promt.

Can anyone there explain it?

Thanks,

Li

I run the same script from irb first, then run it from window promt
#########using irb
C:\>irb
irb(main):001:0> path='C:\Ruby\self'
=> "C:\\Ruby\\self"
irb(main):002:0>
irb(main):003:0* Dir.entries(path).each do |filename|
irb(main):004:1*
irb(main):005:1* if filename=~/(LIPID5-2)(\.\w+)/i
irb(main):006:2>
irb(main):007:2* open(filename).each{|line| puts line}.close
irb(main):008:2>
irb(main):009:2* new_name=filename.gsub(/LIPID5-2/,'LIPID5-3')
irb(main):010:2> File.rename( filename,new_name)
irb(main):011:2>
irb(main):012:2* end
irb(main):013:1>
irb(main):014:1*
irb(main):015:1* end
Errno::ENOENT: No such file or directory - LIPID5-2.001
from (irb):7:in `initialize'
from (irb):7:in `open'
from (irb):7
from (irb):3:in `each'
from (irb):3

######running from window promt
>ruby dir10.rb
>1
20.0, 1433.33333333333, 2027.0
>2
10.0, 2223.36081942031, 3440.9456549036
>Exit code: 0

--
Posted via http://www.ruby-....

1 Answer

Paul Lutus

11/26/2006 11:08:00 PM

0

Li Chen wrote:

> Hi all,
>
> I just write a script to change the filenames within a folder. Out of
> curiosity I want to see the differences bewteen using irb and window
> promt. I run the script from the window promt and I get what I want. But
> when I paste all the codes to irb and run from there I get error
> messages. In theory there is no defference using either irb or running
> from window promt.
>
> Can anyone there explain it?

/ ...

What you don't understand is that you have to choose a working directory,
and "move" there (or always use full paths). Saying "path=/path" isn't good
enough, because it doesn't actually do anything except set the value of a
string.

When you ran your script, you were in the desired directory. When you ran
irb, you were not.

When you invoke "Dir.entries(path)", the result is a set of file names
without paths. It is up to you to complete the paths, either by:

1. Moving to the desired directory, or

2. Using complete paths for each file name.

It is like saying "I want address 123". Without knowing the city and street
names, the address is of no value. It's the same with file paths.

Please keep this in mind -- it is very important. If you do not explicitly
set a path or use full paths, your script will eventually do something you
did not intend.

The worst, most nightmarish example is this:

`rm -rf *` # NEWBIES, NEVER DO THIS

NEVER, EVER execute this command without first setting the path you intend.

If you set a path before issuing the above command, only the files in the
selected directory will be deleted. If you do not set a path, and if you
are not naturally lucky, all your files -- your entire file system -- will
be deleted without warning.

--
Paul Lutus
http://www.ara...