Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > File manipulation

Reply
Thread Tools

File manipulation

 
 
Tim Mcd
Guest
Posts: n/a
 
      07-31-2008
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.)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      07-31-2008
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
 
Reply With Quote
 
 
 
 
Shadowfirebird
Guest
Posts: n/a
 
      07-31-2008
Hi Robert,

It seems to me that that's a hell of a difficult thing to do.

Take your Sword.new() call. There are a lot of different ways I
might code that (especially in Ruby!). A simple search and replace
will probably not be very reliable.

s = Sword.new()
OR
swords = []; swords << sword.new()
OR
Event.new(Sword.new.strike(:martin))

...or whatever. And that's just one statement -- it says nothing of
the multitude of different ways that a given problem can be solved
using Ruby code.

Is there any particular reason why you need to translate Ruby to LPC?
Why not just use Ruby as the in-game language?


(Confession time: I'm writing a MUSH myself, although mine is really
just a learning exercise, and, I suspect, a great deal less ambitious
than your project. For various reasons I'd dismissed the idea of
users writing code within the game, one of them being that I couldn't
see of any easy way to protect the game from malicious code. I'd be
interested on your thoughts on this, either inside or outside the
list.)

Shadowfirebird.


On Thu, Jul 31, 2008 at 7:49 PM, Robert Klemme
<(E-Mail Removed)> wrote:
> 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
>
>




--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

 
Reply With Quote
 
Shadowfirebird
Guest
Posts: n/a
 
      07-31-2008
Sorry: I should have said, "Hi Tim".

On Thu, Jul 31, 2008 at 9:48 PM, Shadowfirebird
<(E-Mail Removed)> wrote:
> Hi Robert,
>
> It seems to me that that's a hell of a difficult thing to do.
>
> Take your Sword.new() call. There are a lot of different ways I
> might code that (especially in Ruby!). A simple search and replace
> will probably not be very reliable.
>
> s = Sword.new()
> OR
> swords = []; swords << sword.new()
> OR
> Event.new(Sword.new.strike(:martin))
>
> ...or whatever. And that's just one statement -- it says nothing of
> the multitude of different ways that a given problem can be solved
> using Ruby code.
>
> Is there any particular reason why you need to translate Ruby to LPC?
> Why not just use Ruby as the in-game language?
>
>
> (Confession time: I'm writing a MUSH myself, although mine is really
> just a learning exercise, and, I suspect, a great deal less ambitious
> than your project. For various reasons I'd dismissed the idea of
> users writing code within the game, one of them being that I couldn't
> see of any easy way to protect the game from malicious code. I'd be
> interested on your thoughts on this, either inside or outside the
> list.)
>
> Shadowfirebird.
>
>
> On Thu, Jul 31, 2008 at 7:49 PM, Robert Klemme
> <(E-Mail Removed)> wrote:
>> 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
>>
>>

>
>
>
> --
> Me, I imagine places that I have never seen / The colored lights in
> fountains, blue and green / And I imagine places that I will never go
> / Behind these clouds that hang here dark and low
> But it's there when I'm holding you / There when I'm sleeping too /
> There when there's nothing left of me / Hanging out behind the
> burned-out factories / Out of reach but leading me / Into the
> beautiful sea
>
>




--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

 
Reply With Quote
 
Tim Mcd
Guest
Posts: n/a
 
      07-31-2008
Hmm... could I possibly see your MUSH code? I have pretty much stuck to
Interactive Fiction with ruby (afraid of networking!) I was wanting to
port Ruby to LPC just for the fun of it. Being able to create rooms on
my MUD with Ruby would be fun! ^_^ Oh, and Shadowfirebird, my email is:
http://www.velocityreviews.com/forums/(E-Mail Removed)

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

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      07-31-2008
On Thu, Jul 31, 2008 at 1:48 PM, Shadowfirebird
<(E-Mail Removed)> wrote:
> Event.new(Sword.new.strike(:martin))


hey!!!!

martin

 
Reply With Quote
 
shadowfirebird@gmail.com
Guest
Posts: n/a
 
      08-01-2008
Event.new(shadowfirebird.cast(:heal, :martin))

On 8/1/08, Martin DeMello <(E-Mail Removed)> wrote:
> On Thu, Jul 31, 2008 at 1:48 PM, Shadowfirebird
> <(E-Mail Removed)> wrote:
>> Event.new(Sword.new.strike(:martin))

>
> hey!!!!
>
> martin
>
>



--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

 
Reply With Quote
 
shadowfirebird@gmail.com
Guest
Posts: n/a
 
      08-01-2008
Well, my current working code is only a proof-of-concept thing and
extremely icky; one big file, with a bunch of stuff hardcoded. But
you can walk around, pick up and drop stuff, dig rooms, and create
items. For better or worse, I'll forward it to you.

My own inspiration came from Jon Lambert's TeensyMud:
http://sourcery.dyndns.org/wiki.cgi?TeensyMud

And, even better, this post, again by Jon, that does a MUD in 15 lines
of Ruby(!): http://redhanded.hobix.com/bits/mudIn15LinesOfRuby.html

If you're listening, Jon, thanks.

Shadowfirebird.

On 7/31/08, Tim Mcd <(E-Mail Removed)> wrote:
> Hmm... could I possibly see your MUSH code? I have pretty much stuck to
> Interactive Fiction with ruby (afraid of networking!) I was wanting to
> port Ruby to LPC just for the fun of it. Being able to create rooms on
> my MUD with Ruby would be fun! ^_^ Oh, and Shadowfirebird, my email is:
> (E-Mail Removed)
>
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

 
Reply With Quote
 
Ian Hobson
Guest
Posts: n/a
 
      08-01-2008
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.)
>

Hi Tim,

I suspect your phrase replacement scheme will not cover all your needs.
You need a more powerfull technique.

May I suggest you structure it like a compiler. Use a tree structure for
the first intermediate form, generated (with dictionary of names or
symbol table) from the first pass. The second pass would walk this tree,
calling first level semantic routines for each node. These would, in
their turn call routines (methods in an object) that knows about LPC.
The interface between the first and second level back end is important,
but need never actually go to disk.

Not knowing the structure of the files, I can't say how much of the
structure of Ruby you will have to include. Until that is established,
you can't decide if you are going to go top-down or bottom-up - see any
book on compilers. I find top-down easeir to undertand, generates more
meaningsul error messages, and has better error recovery than bottom-up,
but that may be my brain.

By replacing one set of LPC routines with another set, you get an
interpreter of the file. This is the same as replacing the LPC object
with an interpreting object. Note, if you call the semantic routines
earlier, instead of creating the tree form, then compiling may be
quicker, but interpretation becomes impossible. You can't recompile each
time through a loop, becasue you get problems with the state being
changed as well as slow performance.

You might also look at what the LPC compiler actually produces and
generate the same. This will give you a way to write your scripts in (a
subset of) Ruby.

Regards

Ian

 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      08-01-2008
>> Event.new(Sword.new.strike(:martin))
>
> hey!!!!
>
> martin


No worries. It's just a symbol.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Resource File Manipulation JezB ASP .Net 0 02-25-2005 05:27 PM
HELP: looking for ElfTree-like file manipulation program for WinXP wege Computer Support 4 06-04-2004 03:36 AM
Zip file manipulation Roedy Green Java 2 08-17-2003 06:12 AM
Re: file overwrite data manipulation John Harrison C++ 2 06-27-2003 05:12 AM
Re: file overwrite data manipulation Chris Theis C++ 0 06-26-2003 07:32 AM



Advertisments