# \[SOLUTION\] Lisp Game (#49)

**URL:** <https://rubytalk.org/t/solution-lisp-game-49/21272>\
**Category:** ruby-talk\
**Created:** [2 October 2005 22:41 UTC](https://rubytalk.org/t/solution-lisp-game-49/21272 "2005-10-02T22:41:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Markus\_Koenig](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@Markus\_Koenig](https://rubytalk.org/u/Markus_Koenig)\
**Post date:** [2 October 2005 22:41 UTC](https://rubytalk.org/t/solution-lisp-game-49/21272/1 "2005-10-02T22:41:48Z")

</div>

Again a fun quiz! I remember reading this tutorial too.

This is my solution, which uses Highline (version 1.\* recommended). My  
approach was far more rewrite-based. Unfortunately, I didn't come up  
with a nice game-action "macro".

The user interface is the same as the Lisp program's, but omitting the  
parens. BTW, why does Highline lose when EOF is encountered? I really  
miss the Ctrl-D...

Anyway, I noticed one could cheat in my game:

> > take bucket  
> > instance\_variable\_set @bucket\_filled foobar  
> > splash bucket wizard

Voila. 😉

Markus

> **···**
>
> ======
> 
> #! /usr/bin/env ruby
> 
> require 'highline/import'  
> require 'set'
> 
> # This is a kind of room, including a garden.  
> class Room  
> &nbsp;&nbsp;def initialize(description, \*items)  
> &nbsp;&nbsp;&nbsp;&nbsp;@description = description  
> &nbsp;&nbsp;&nbsp;&nbsp;@directions = Hash.new  
> &nbsp;&nbsp;&nbsp;&nbsp;@direction\_gateways = Hash.new  
> &nbsp;&nbsp;&nbsp;&nbsp;@inventory = Set.new(items)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def look  
> &nbsp;&nbsp;&nbsp;&nbsp;puts @description  
> &nbsp;&nbsp;&nbsp;&nbsp;@direction\_gateways.each do |dir, gateway|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "There is a #{gateway} going #{dir} from here."  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;@inventory.each do |item|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "You see a #{item} on the floor."  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def define\_direction(dir, gateway, where)  
> &nbsp;&nbsp;&nbsp;&nbsp;@directions[dir] = where  
> &nbsp;&nbsp;&nbsp;&nbsp;@direction\_gateways[dir] = gateway  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;# returns the place a direction leads to  
> &nbsp;&nbsp;def get\_direction(dir)  
> &nbsp;&nbsp;&nbsp;&nbsp;@directions[dir]  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def have?(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;@inventory.include? item  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def drop(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;@inventory.add item  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def take(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;@inventory.delete item  
> &nbsp;&nbsp;&nbsp;&nbsp;return item  
> &nbsp;&nbsp;end  
> end
> 
> # Define the rooms with items in them.  
> $livingroom = Room.new("You are in the living-room of a wizard's  
> house.\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;"There is a wizard snoring loudly on the couch.", :bucket)  
> $attic = Room.new("You are in the attic of the abandoned house.\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;"There is a giant welding torch in the corner.")  
> $garden = Room.new("You are in a beautiful garden.\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;"There is a well in front of you.", :chain, :frog)
> 
> # Connect the rooms.  
> $livingroom.define\_direction :west, :door, $garden  
> $livingroom.define\_direction :upstairs, :stairway, $attic  
> $attic.define\_direction :downstairs, :stairway, $livingroom  
> $garden.define\_direction :east, :door, $livingroom
> 
> # This is the guy you control.  
> class Apprentice  
> &nbsp;&nbsp;# texts that are shown at The End  
> &nbsp;&nbsp;ENDGAME\_WIN =  
> &nbsp;&nbsp;&nbsp;&nbsp;"The wizard awakens from his slumber and greets you\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;"warmly. He hands you the magic low-carb donut -\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;"You win! The End."  
> &nbsp;&nbsp;ENDGAME\_LOSE =  
> &nbsp;&nbsp;&nbsp;&nbsp;"The wizard awakens and sees that you stole his frog.\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;"He is so upset he banishes you to the netherworlds -\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;"You lose! The End."  
> &nbsp;&nbsp;ENDGAME\_EXIT =  
> &nbsp;&nbsp;&nbsp;&nbsp;"The wizard awakens, puling disappointedly,\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;" Premature disassociation is the root\n" +  
> &nbsp;&nbsp;&nbsp;&nbsp;" Of all eval."
> 
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;@location = $livingroom  
> &nbsp;&nbsp;&nbsp;&nbsp;@inventory = Set.new([:"whiskey-bottle"])
> 
> &nbsp;&nbsp;&nbsp;&nbsp;@chain\_welded = false  
> &nbsp;&nbsp;&nbsp;&nbsp;@bucket\_filled = false  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;# \*\*\* Commands for exploring the house \*\*\*
> 
> &nbsp;&nbsp;def exit  
> &nbsp;&nbsp;&nbsp;&nbsp;# show the wizard's wisdom  
> &nbsp;&nbsp;&nbsp;&nbsp;puts ENDGAME\_EXIT
> 
> &nbsp;&nbsp;&nbsp;&nbsp;# if this was plain "exit", we had infinite recursion...  
> &nbsp;&nbsp;&nbsp;&nbsp;Kernel.exit  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;alias quit exit
> 
> &nbsp;&nbsp;def look  
> &nbsp;&nbsp;&nbsp;&nbsp;@location.look  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def walk(dir)  
> &nbsp;&nbsp;&nbsp;&nbsp;if @location.get\_direction(dir)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@location = @location.get\_direction(dir)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@location.look  
> &nbsp;&nbsp;&nbsp;&nbsp;elsif @location == $garden and dir.to\_s.hash == -781591621  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# well, how do you get here?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'You see a maze of twisty little passages,'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'all alike. But you can\'t go there.'  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "You can't go #{dir}."  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;alias go walk
> 
> &nbsp;&nbsp;# \*\*\* Commands for handling items \*\*\*
> 
> &nbsp;&nbsp;# note: have? does not print anything  
> &nbsp;&nbsp;def have?(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;@inventory.include? item  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def have(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;puts have?(item)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def drop(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;if have? item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@location.drop item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@inventory.delete item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "You are no longer carrying the #{item}."  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "You do not have that."  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def take(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;if @location.have? item  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@inventory.add @location.take(item)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "You are now carrying the #{item}."  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "I see no #{item} here."  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;alias pickup take
> 
> &nbsp;&nbsp;def inventory  
> &nbsp;&nbsp;&nbsp;&nbsp;if @inventory.empty?  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'You are carrying no items.'  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inventory\_text = @inventory.to\_a.join(', ')  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "You are carrying: #{inventory\_text}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if have? :bucket and @chain\_welded  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'The chain is welded to the bucket.'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if have? :bucket and @bucket\_filled  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'The bucket is filled with water.'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;# \*\*\* Game actions \*\*\*
> 
> &nbsp;&nbsp;def weld(subject, object)  
> &nbsp;&nbsp;&nbsp;&nbsp;if @location == $attic and have? :chain and have? :bucket and  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject == :chain and object == :bucket  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@inventory.delete :chain  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@chain\_welded = true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'The chain is now securely welded to the bucket.'  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'You cannot weld like that.'  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def dunk(subject, object)  
> &nbsp;&nbsp;&nbsp;&nbsp;if @location == $garden and have? :bucket and  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject == :bucket and object == :well  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @bucket\_filled  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'The bucket is already filled.'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif @chain\_welded  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@bucket\_filled = true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'The bucket is now full of water.'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'The water level is too low to reach.'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'You cannot dunk like that.'  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def splash(subject, object)  
> &nbsp;&nbsp;&nbsp;&nbsp;if @location == $livingroom and have? :bucket and  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subject == :bucket and object == :wizard  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if not @bucket\_filled  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'The bucket has nothing in it.'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif have? :frog  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ENDGAME\_LOSE  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kernel.exit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ENDGAME\_WIN  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Kernel.exit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'You cannot splash like that.'  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> # Create the apprentice.  
> $apprentice = Apprentice.new  
> $apprentice.look
> 
> # Provide a user interface.  
> loop do  
> &nbsp;&nbsp;# read a line  
> &nbsp;&nbsp;command = ask('\>\> ') do |question|  
> &nbsp;&nbsp;&nbsp;&nbsp;# try to use Readline (only Highline 1.0.0 and above)  
> &nbsp;&nbsp;&nbsp;&nbsp;if question.respond\_to? :readline=  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;question.readline = true  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;# run the command  
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;commandwords = command.split.map {|x| x.downcase.to\_sym}  
> &nbsp;&nbsp;&nbsp;&nbsp;$apprentice.send \*commandwords unless commandwords.empty?  
> &nbsp;&nbsp;rescue ArgumentError, NoMethodError  
> &nbsp;&nbsp;&nbsp;&nbsp;puts 'I do not understand.'  
> &nbsp;&nbsp;end  
> end

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [2 October 2005 23:46 UTC](https://rubytalk.org/t/solution-lisp-game-49/21272/2 "2005-10-02T23:46:46Z")

</div>

Markus Koenig wrote:

> The user interface is the same as the Lisp program's, but omitting the  
> parens. BTW, why does Highline lose when EOF is encountered? I really  
> miss the Ctrl-D...

Hmm... not exactly sure why this doesn't work. I'll try to dig through  
HighLine and figure this out if I get some time this week, if James  
doesn't beat me to the punch, that is 🙂

---

<div class="post-metadata">

**Author:** ![Markus\_Koenig](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@Markus\_Koenig](https://rubytalk.org/u/Markus_Koenig)\
**Post date:** [3 October 2005 00:36 UTC](https://rubytalk.org/t/solution-lisp-game-49/21272/3 "2005-10-03T00:36:47Z")

</div>

Gregory Brown wrote:

> Markus Koenig wrote:
> 
> > The user interface is the same as the Lisp program's, but omitting the  
> > parens. BTW, why does Highline lose when EOF is encountered? I really  
> > miss the Ctrl-D...
> 
> Hmm... not exactly sure why this doesn't work. I'll try to dig through  
> HighLine and figure this out if I get some time this week, if James  
> doesn't beat me to the punch, that is 🙂

This is what I get from HighLine 1.0.1:

&nbsp;&nbsp;&nbsp;&nbsp;[...]  
&nbsp;&nbsp;&nbsp;&nbsp;/usr/lib/ruby/site\_ruby/1.8/highline/question.rb:362:in `send':  
&nbsp;&nbsp;&nbsp;&nbsp;undefined method `strip' for nil:NilClass (NoMethodError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/site\_ruby/1.8/highline/question.rb:362:in `remove\_whitespace'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/site\_ruby/1.8/highline.rb:527:in `get\_line'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/site\_ruby/1.8/highline.rb:545:in `get\_response'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/site\_ruby/1.8/highline.rb:155:in `ask'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from lispgame.rb:221  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from lispgame.rb:219:in `loop'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from lispgame.rb:219

In question.rb:362 the result of readline (highline.rb:528) or gets  
(highline.rb:533) is stripped. I guess the cleanest thing to do is just  
to raise EOFError if any of these methods returns nil.

This wasn't that hard 🙂

Regards,  
Markus

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [3 October 2005 15:45 UTC](https://rubytalk.org/t/solution-lisp-game-49/21272/4 "2005-10-03T15:45:22Z")

</div>

Hello Markus.

I'm very interested in understanding this problem and fixing it correctly. Could you please show a trivial HighLine script and how you are trying to use it, so I can wrap my head around this?

Thanks.

James Edward Gray II

> **···**
>
> On Oct 2, 2005, at 7:36 PM, Markus Koenig wrote:
> 
> > Gregory Brown wrote:
> > 
> > > Markus Koenig wrote:
> > > 
> > > > The user interface is the same as the Lisp program's, but omitting the  
> > > > parens. BTW, why does Highline lose when EOF is encountered? I really  
> > > > miss the Ctrl-D...
> > > 
> > > Hmm... not exactly sure why this doesn't work. I'll try to dig through  
> > > HighLine and figure this out if I get some time this week, if James  
> > > doesn't beat me to the punch, that is 🙂
> > 
> > This is what I get from HighLine 1.0.1:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;[...]  
> > &nbsp;&nbsp;&nbsp;&nbsp;/usr/lib/ruby/site\_ruby/1.8/highline/question.rb:362:in `send':  
> > &nbsp;&nbsp;&nbsp;&nbsp;undefined method `strip' for nil:NilClass (NoMethodError)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/site\_ruby/1.8/highline/question.rb:362:in `remove\_whitespace'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/site\_ruby/1.8/highline.rb:527:in `get\_line'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/site\_ruby/1.8/highline.rb:545:in `get\_response'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from /usr/lib/ruby/site\_ruby/1.8/highline.rb:155:in `ask'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from lispgame.rb:221  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from lispgame.rb:219:in `loop'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from lispgame.rb:219
> > 
> > In question.rb:362 the result of readline (highline.rb:528) or gets  
> > (highline.rb:533) is stripped. I guess the cleanest thing to do is just  
> > to raise EOFError if any of these methods returns nil.

---

<div class="post-metadata">

**Author:** ![Markus\_Koenig](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@Markus\_Koenig](https://rubytalk.org/u/Markus_Koenig)\
**Post date:** [6 October 2005 21:11 UTC](https://rubytalk.org/t/solution-lisp-game-49/21272/5 "2005-10-06T21:11:53Z")

</div>

Hello James,

I really appreciate your and Greg's work. It's nice to see what can  
evolve from a simple quiz idea. 🙂

> I'm very interested in understanding this problem and fixing it  
> correctly. Could you please show a trivial HighLine script and how  
> you are trying to use it, so I can wrap my head around this?

I think HighLine's methods should either raise EOFError or return nil  
if EOF is reached, but I'm not sure which one would be nicer. Maybe a  
hybrid approach, like IO#gets and IO#readline?

The trivial script, if the methods return nil:

&nbsp;&nbsp;&nbsp;&nbsp;require 'highline/import'  
&nbsp;&nbsp;&nbsp;&nbsp;loop do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = ask("What's your name?")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if response.nil? or response == 'exit'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Goodbye, dear friend.'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello, #{name}!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

I expect this program to behave like most Unix shells - quietly quit  
when one presses Ctrl-D (EOF). But this would print a stack trace, as  
shown earlier.

Thanks,  
Markus

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [9 October 2005 22:01 UTC](https://rubytalk.org/t/solution-lisp-game-49/21272/6 "2005-10-09T22:01:27Z")

</div>

> Hello James,
> 
> I really appreciate your and Greg's work. It's nice to see what can  
> evolve from a simple quiz idea. 🙂

Thanks for the kind words.

> > I'm very interested in understanding this problem and fixing it  
> > correctly. Could you please show a trivial HighLine script and how  
> > you are trying to use it, so I can wrap my head around this?
> 
> I think HighLine's methods should either raise EOFError or return nil  
> if EOF is reached, but I'm not sure which one would be nicer. Maybe a  
> hybrid approach, like IO#gets and IO#readline?
> 
> The trivial script, if the methods return nil:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;require 'highline/import'  
> &nbsp;&nbsp;&nbsp;&nbsp;loop do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = ask("What's your name?")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if response.nil? or response == 'exit'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Goodbye, dear friend.'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello, #{name}!"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> I expect this program to behave like most Unix shells - quietly quit  
> when one presses Ctrl-D (EOF). But this would print a stack trace, as  
> shown earlier.

I've added this to our TODO. It will be addressed in the next version of HighLine.

James Edward Gray II

> **···**
>
> On Oct 6, 2005, at 4:11 PM, Markus Koenig wrote:
