[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6442] Re: variable in regular expression

From: David Alan Black <dblack@...>
Date: 2000-11-18 17:46:55 UTC
List: ruby-talk #6442
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


In This Thread