The three rules of Ruby Q.:
-
Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message. -
Support Ruby Q. by submitting ideas as often as you can:
- Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby T. follow the discussion. Please reply to the original quiz
message,
if you can.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Caleb T.
I remember playing with some old AI programs of which you could ask
questions
and if it didn’t know the answer, it would ask you what the answer is:
Hi, I'm an AI program. What's your question?
>> How are you today?
I'm afraid I don't know the answer to that. Please tell me what I
should say.
>>> Just fine, thanks.
Okay, I will remember that. Please ask a question.
>>> How are you today?
Just fine, thanks.
Please ask another question.
This got me thinking about an interesting concept in Ruby.
Your Quiz: Write a Ruby module that can be mixed into a class. The
module does
the following: upon receiving a method call that is unknown to the
class, have
Ruby inform the user that it doesn’t know that particular method call.
Then,
have it ask the user for Ruby code as input to use for that method call
the next
time it is called.
Example:
> object.some_unknown_method
some_unknown_method is undefined
Please define what I should do (end with a newline):
>> @foo = 5
>> puts "The value of foo is #{@foo}"
>>
Okay, I got it.
>>> object.some_unknown_method
"The value of foo is 5"
[Editor’s Note:
I envision this could actually be handy for prototyping classes IRb.
Bonus
points if you can later print the source for all methods interactively
defined.
–JEG2]