From: Robert Klemme Date: 2008-08-01T03:49:12+09:00 Subject: Re: File manipulation On 31.07.2008 20:22, Tim Mcd wrote: > So, I am currently working on a MUD (Online multiplayer text based game, > like an MMO). The MUD's scripting language is LPC. I am trying to create > a ruby script, that reads another ruby file, and changes all instances > of 'Sword-of-might = Sword.new(...)' to something like 'static void > create() {...}'. Nothing too fancy, just me being able to go through a > file finding different bits of ruby that I can then replace with the > equivalent LPC code. > > An example of how it might work is: Ruby has a table of ruby phrases and > what to turn them into if encountered. Ruby then reads the given file, > and if it finds any of the given phrases, turns them into the > corresponding LPC that I have defined. > > So, the problem is, I have no idea how to go about doing something > like this in Ruby. Any thoughts/help or places to look? (tutorials, > etc.) Depends how many phrases you got there. If there are just a few of them and they are fixed, you can do something like this: phrases = { "foo" => "bar", "baz" => "boo!", } dat = File.read input_file dat.gsub! Regexp.union(phrases.keys) do |m| phrases[m] or raise "Phrase not found: #{m.inspect}" end File.open out, "w" do |io| io.write dat end Kind regards robert