[Quiz] [Solution] Lisp Game

  • Thread starter Brian Schröder
  • Start date
B

Brian Schröder

Hello Group,

Nice quiz James!

I refreshed and added to my lisp knowledge and had fun with adventure.
I did a one to one translation from the lisp code to ruby code. I used
a similar user interface targeting irb as proposed.

Using a method_missing hack I allowed things like

pickup bottle

or even

splash bottle frog

to work.

Then I rewrote the whole thing in ruby style using a readline
interface and adding some capabilities. I think it came out quite nice
and I juggled a lot with meta programming concepts. So thanks to all
the helpfull people here on the list I have learned a lot this week.

The solution can be found and browsed at

http://ruby.brian-schroeder.de/quiz/adventure/

best regards,

Brian
 
D

Dave Burt

Brian Schröder:
...
or even

splash bottle frog

to work.

Then I rewrote the whole thing in ruby style using a readline
interface and adding some capabilities. I think it came out quite nice
and I juggled a lot with meta programming concepts. So thanks to all
the helpfull people here on the list I have learned a lot this week.

I've enjoyed reading both versions of your code. It looks a lot more
comfortable than my attempts at retaining the LISP code's semantics.

The define_game_action method is particularly interesting: using "subject,
object = *args.flatten" to un-collect arguments from a passed array (built
by a magic method-missing) is cool. The coolest bit, though, is passing the
parameter (local) variable action to instance_eval inside the define_method
block. I don't quite get how that works; if you read my code, you'll see I
defined that function into a separate method. I thought methods were banned
from accessing local variables from outside their scope - this one seems to
be staying in like in a closure.

I'm glad to see evidence in the "Eval and block problems" thread that it's
harder than it looks for other people, too :)

Cheers,
Dave
 
J

James Edward Gray II

The coolest bit, though, is passing the parameter (local) variable
action to instance_eval inside the define_method block. I don't
quite get how that works; if you read my code, you'll see I
defined that function into a separate method. I thought methods
were banned from accessing local variables from outside their scope
- this one seems to be staying in like in a closure.

define_method() builds a method from the provided block and all Ruby
blocks are closures. That's why it behaves differently, I think.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top