From: "HarryO" <harryo / zipworld.com.au>
>
> On Sun, 06 Jan 2002 10:21:32 +1100, Mark Hahn wrote:
> 
> > Imagine that you want to save text back to a file, but only if it
> > changed:
> > 
> > if str.upcase! then File.open('x.txt','w'){|f| f.write str} end
> 
> I can certainly see that's a use for it ... but what's the likelihood
> someone would really want to attempt to change something to uppercase
> and only write it to a file if it changed :-).

Well admittedly upcase seems an unlikely [real-world]
candidate for the direct file-write-if-modified situation
presented in the example above, but I've defintely done
this with gsub!.

. . . for what it's worth . . . . And in thinking about
it, although I haven't done this in Ruby yet, in a real-
world sense, anyway, one could chain multiple operations
together, transformations potentially executed on a 
particular string, keeping track if any along the line
had resulted in a modification:

  altered = false
  altered |= data.gsub!(/tim/, "enchanter")
  altered |= data.gsub!(/doug/, "dinsdale")
  altered |= data.upcase!  # well, why not =)
  # etc...

... you know, then finally write the file IFF altered.



Regards,

Bill