Skip to content

Instantly share code, notes, and snippets.

@Oshuma
Created January 16, 2009 00:47
Show Gist options
  • Select an option

  • Save Oshuma/47740 to your computer and use it in GitHub Desktop.

Select an option

Save Oshuma/47740 to your computer and use it in GitHub Desktop.
Sinatra powered Rock, Paper, Scissors!
# Sinatra powered Rock, Paper, Scissors!
require 'rubygems'
require 'sinatra'
configure do
TITLE = 'Sinatra powered Rock, Paper, Scissors!'
RPS = %w(rock paper scissors)
RPSSL = RPS + %w(spock lizard)
end
get '/' do
set_header
@choice = RPS.sort_by {rand}.first
@alternate_link = "<a href='/rpssl'>Rock, Paper, Scissors, Spock, Lizard</a>"
erb :rps
end
# Rock, Paper, Scissors, Spock, Lizard
# http://www.samkass.com/theories/RPSSL.html
get '/rpssl' do
set_header
@choice = RPSSL.sort_by {rand}.first
@alternate_link = "<a href='/'>Rock, Paper, Scissors (old school)</a>"
erb :rpssl
end
private
def set_header
header 'Content-Type' => 'text/html; charset=utf-8'
end
use_in_file_templates!
__END__
@@ layout
<html>
<head>
<title><%= TITLE %></title>
<style type="text/css">
body {
background-color: #95b7dc;
font-family: "helvetica";
margin: 1em auto;
text-align: center;
width: 700px;
}
a {
border-bottom: 1px dashed #6a4823;
color: #eee;
text-decoration: none;
}
a:hover { border-bottom: 1px solid #6a4823; }
.header {
color: #6a4823;
}
.choice {
font-size: 4em;
font-weight: bold;
}
</style>
</head>
<body>
<h1 class="header">Sinatra chooses:</h1>
<div class="choice">
<%= yield %>
</div>
<p><a href="">Go again!</a></p>
<p>Or play <%= @alternate_link %></p>
</body>
</html>
@@ rps
<%= @choice %>
@@ rpssl
<%= @choice %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment