This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: MUD in 15 Lines of Ruby
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
This is great. In response to a post about how writing a MUD in Ruby would be “a horrifically complex and lengthy project,” Jon Lambert dropped a 15-line MUD in the response! Now that’s a comeback.
require 'socket';require 'yaml';def q x;$m.find{|o|o.t==:p&&x==o.n};end
def a r,t;$m.find_all{|o|t==o.t&&(!r||r==o.l)};end;def g z;a(nil,:p).each{|p|
p.p z};end;class O;attr_accessor :i,:n,:l,:e,:s,:t;def initialize n,l=nil,t=:r
@n,@l,@i,@t=n,l,$d+=1,t;@e={};end;def p s;@s.puts(s)if @s;end;def y m
v=$m.find{|o|@l==o.i};t=v.e.keys;case m;when/^q/;@s.close;@s=nil;
File.open('d','w'){|f|YAML::dump $m,f};when/^h/;p "i,l,d,g,c,h,q,,O,R"
when/^i/;a(@i,:o).each{|o|p o.n};when/^c.* (.*)/;g "#{@n}:#{$1}"
when/^g/;a(@l,:o).each{|q|q.l=@i};when/^d/;a(@i,:o).each{|q|q.l=@l}
when/^O (.*)/;$m<<<
A bit of sleuthing on the matter and I’ve discovered that this is a derivative of Jon’s TeensyMud. He has nice, readable versions in his repository, if you like.
If you want to play with this one:
Save the above to teensymud.rb.
Create a YAML file named “d” (for dungeon) with a blank array in it. (Likeso: echo "[]" > d.)
Run teensymud.rb.
Telnet to localhost port 4000.
At Name prompt, enter your name then .
Play commands
i
displays player inventory
l
displays the contents of a room
dr
drops all objects in your inventory into the room
g
gets all objects in the room into your inventory
k
attempts to kill player (e.g. k bubba)
s[ay]
sends to all players in the room
c[hat]
sends to all players in the game
q[uit]
quits the game (saves player)
moves player through exit named (ex. south)
OLC
O
creates a new object (ex. O rose)
R
creates a new room and autolinks the exits using the exit names provided.
Please, tell us more, Jon. If you’re feeling nostalgic and want to egg things on, come join ruby-talk this week. The Ruby Quiz will be a MUD client. (extracted from ruby-talk:154208.)