Created
August 22, 2012 12:42
-
-
Save migane/3425206 to your computer and use it in GitHub Desktop.
Text reverse, palindrome, concatenation in sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Main file string_reverse.rb | |
require 'sinatra' | |
helpers do | |
def concatenate_strings(string1, string2) | |
string1 + string2 | |
end | |
def is_a_palindrome?(string1, string2) | |
string1==string2 | |
end | |
end | |
get '/' do | |
erb :home | |
end | |
post '/reverse' do | |
erb :show | |
end | |
# View files: views/home.erb | |
<h1>String Reverse</h1> | |
<p>The String reverse Service will accept a string from you | |
and return you the reversed string. Have fun!</p> | |
<form action="/reverse" method="post" accept-charset="utf-8"> | |
<input type="text" name="str" value="Type a string..." id="str"> | |
<input type="submit" value="... and reverse it!"> | |
</form> | |
# View file: views/show.erb | |
h1>String Reverse results</h1> | |
<% initial_string = params[:str] %> | |
<% result_string = initial_string.reverse %> | |
<% concatenated_strings = concatenate_strings(initial_string, result_string) %> | |
<% palindrome_string = is_a_palindrome?(result_string, initial_string ) %> | |
<p>You had entered the string - <%= initial_string %></p> | |
<p>The reversed string is - <%= result_string %></p> | |
<p>The concatenation of the two strings is : <%= concatenated_strings %></p> | |
<p><%= result_string %> is <% if !(palindrome_string) %> not <% end %> a palindrome of <%= initial_string%></p> | |
<p>Click <a href="/">here</a> to try a new string.</p> | |
# Note: Probably a better way than putting all initialization in view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment