gem install konamio
and
require "konamio"
> Konamio::Sequence::Requisition.new.execute!
Enter konami code (or hit escape)
Good job, you.
=> #<Konamio::Result:0x937ddb4
@data={:data=>{:confirmation=>"Good job, you."}},
@success=true>
You can configure it to listen for any ascii based sequence you want
using the :sequence
option. This can be specified using a string
("foobar"
) or an array of recognized symbols and one-character strings
(escaped charactersare okay, e.g. [:up, "1", "2", "3", "\t"]
):
> Konamio::Sequence::Requisition.new({
sequence: "a".upto("z").to_a.reverse,
prompt: "Say the alphabet backwards",
confirmation: "Okay, you can go"
}).execute!
Say the alphabet backwards
Okay, you can go
=> #<Konamio::Result:0x9265788
@data={:data=>{:confirmation=>"Okay, you can go"}},
@success=true>
There are three dialogs that Konamio might send to stdout.
:prompt
is the dialog displayed initially, and each time the user fails to supply the proper sequence.:confirmation
is displayed when the required sequence is entered properly.:cancellation
is displayed when user terminates by pressing the escape key.
You can customize any of these dialogs, or disable them individually by passing a falsey value.
It would be boring if all Konamio did was return a result object. That's why
Konamio::Sequence::Requisition#execute!
takes a block, and will execute
that block when the sequence has been successfully entered. You're limited only
by your imagination and the context of your application. Konamio will supply
the value of the sequence to the block.
The following code would prompt the user to enter the konami code twice:
Konamio::Sequence::Requisition.new.execute! { Konamio::Sequence::Requisition.new.execute! }
This would give you +30 lives:
Konamio::Sequence::Requisition.new.execute! { 30.times { puts "+1up" } }
"Password protect" your rails console by doing the following:
- Follow these instructions to obtain a
.irbrc
file that loads your local.irbrc
- Add this gem to your rails project, then create a
.irbrc
file for the project containing
require "konamio"
Konamio::Sequence::Requisition.new(prompt: false, confirmation: false, cancellation: false).execute!
to require console users to enter the konami code. This funny trick obviously does not provide any real protection, but it would certainly make for a nasty practical joke.