Hello --


On Sun, 19 Nov 2000, markus jais wrote:

> hello,
> 
> I have recently bought the new book about ruby and are new trying to learn.
> I have one question about variable in regular expressions
> 
> I have a little script which I want to use to rename files in a directory:
> the code is a follows:
> 
> old = ARGV[1]
> new = ARGV[2]
> dir = Dir.new(".");
> while line = dir.read
>   next if line == "." or line == ".."
>   puts line
>   line.sub!(/old/,  new)    #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Error
>   puts line
> end

In addition to the other (more relevant) advice you've already
received, you might be able to do this more compactly with a fileglob:

  old, new = ARGV[1..2]  # or 0..1, as the case may be

  Dir["*"].each {|s|
    puts s
    s[old] = new         # as per Dave T. -- see his & others' advice
    puts s
  }

(Or does this introduce other problems, e.g. with portability?)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav