L
Louis J Scoras
------=_Part_17982_9256152.1128372048201
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Hi all;
I started to give the lisp quiz a go. From the begining, I resolved to try
for a solution such that the map can be set up in a declarative manner.
I wanted it to look something like the attr_ class methods and ActiveRecord=
s
`associations`; however, setting up something like this seems to be more
work than I thought it would be. Following is some working code, but it's
pretty ugly. Setting the class variable in this manner just feels very
hackish, yet it's the only way I could seem to get this to work.
The problem was this: how to write the methods so that they don't have to b=
e
explicitly overwritten in the subclasses, and yet they can reference these
concrete classes state?
Anyone with a bit more ruby experiance have any refactoring ideas?
--
Lou
module LocationClassMethods
def exit_to(place,direction,opts=3Dnil)
portal =3D (opts[:through] || opts[:via] if opts.respond_to?:[]) || 'door'
class_variable_set@@exits, []) unless class_variables.include?@@
exits.to_s)
exits =3D class_variable_get@@exits)
exits << Exit.new(direction,portal)
end
def description(desc)
class_variable_set@@description, desc)
end
def exits
class_variable_get@@exits)
end
def get_description
class_variable_get@@description)
end
end
class Location
def self.inherited(sub)
sub.extend(LocationClassMethods)
end
def initialize
@exits =3D self.class.exits
@description =3D self.class.get_description
end
def describe
puts @description
describe_exits
end
def describe_exits
@exits.each { |e| puts e.describe }
end
end
class Attic < Location
exit_to :living_room, :down, :via =3D> 'staircase'
description 'You are in the attic of the wizard\'s house. There is a giant
welding torch in the corner.'
end
class Garden < Location
exit_to :living_room, :west
description 'You are standing in a beautiful garden. There is a well in
front of you.'
end
class LivingRoom < Location
exit_to :attic, :up, :via =3D> 'staircase'
exit_to :garden, :east
description 'You are in the living-room of a wizard\'s house. There is a
wizard snoring loudly on the couch.'
end
a =3D Attic.new
a.describe
------=_Part_17982_9256152.1128372048201--
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Hi all;
I started to give the lisp quiz a go. From the begining, I resolved to try
for a solution such that the map can be set up in a declarative manner.
I wanted it to look something like the attr_ class methods and ActiveRecord=
s
`associations`; however, setting up something like this seems to be more
work than I thought it would be. Following is some working code, but it's
pretty ugly. Setting the class variable in this manner just feels very
hackish, yet it's the only way I could seem to get this to work.
The problem was this: how to write the methods so that they don't have to b=
e
explicitly overwritten in the subclasses, and yet they can reference these
concrete classes state?
Anyone with a bit more ruby experiance have any refactoring ideas?
--
Lou
module LocationClassMethods
def exit_to(place,direction,opts=3Dnil)
portal =3D (opts[:through] || opts[:via] if opts.respond_to?:[]) || 'door'
class_variable_set@@exits, []) unless class_variables.include?@@
exits.to_s)
exits =3D class_variable_get@@exits)
exits << Exit.new(direction,portal)
end
def description(desc)
class_variable_set@@description, desc)
end
def exits
class_variable_get@@exits)
end
def get_description
class_variable_get@@description)
end
end
class Location
def self.inherited(sub)
sub.extend(LocationClassMethods)
end
def initialize
@exits =3D self.class.exits
@description =3D self.class.get_description
end
def describe
puts @description
describe_exits
end
def describe_exits
@exits.each { |e| puts e.describe }
end
end
class Attic < Location
exit_to :living_room, :down, :via =3D> 'staircase'
description 'You are in the attic of the wizard\'s house. There is a giant
welding torch in the corner.'
end
class Garden < Location
exit_to :living_room, :west
description 'You are standing in a beautiful garden. There is a well in
front of you.'
end
class LivingRoom < Location
exit_to :attic, :up, :via =3D> 'staircase'
exit_to :garden, :east
description 'You are in the living-room of a wizard\'s house. There is a
wizard snoring loudly on the couch.'
end
a =3D Attic.new
a.describe
------=_Part_17982_9256152.1128372048201--