<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BogoJoker &#187; tutorial</title>
	<atom:link href="http://blog.bogojoker.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.bogojoker.com</link>
	<description>&#039;My humble abode&#039;.sub(/ab/,&#039;c&#039;)</description>
	<lastBuildDate>Mon, 04 Apr 2011 07:33:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Markdown =&gt; Tutorial in 1 Step</title>
		<link>http://blog.bogojoker.com/2009/05/markdown-tutorial-in-1-step/</link>
		<comments>http://blog.bogojoker.com/2009/05/markdown-tutorial-in-1-step/#comments</comments>
		<pubDate>Mon, 18 May 2009 15:00:10 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[markdown]]></category>

		<guid isPermaLink="false">http://blog.bogojoker.com/?p=787</guid>
		<description><![CDATA[When I wrote my Ruby Readline tutorial I felt I came up with a cool concept. I started with a Markdown file, translated it to html, and I used the headers to generate a Table of Contents on the fly. It didn&#8217;t take me long to realize that I could turn this into a framework [...]]]></description>
			<content:encoded><![CDATA[<p>When I wrote my <a href="http://bogojoker.com/readline/">Ruby Readline</a> tutorial I felt I came up with a cool concept.  I started with a Markdown file, translated it to html, and I used the headers to generate a Table of Contents on the fly.</p>
<p><a href="http://github.com/JosephPecoraro/markdownorial/tree/master"><img src="http://blog.bogojoker.com/wp-content/uploads/2009/05/picture-11.png" alt="table of contents" title="table of contents" width="516" height="366" style="border: 1px solid lightGray; margin: 10px 20px;" /></a></p>
<p>It didn&#8217;t take me long to realize that I could turn this into a framework where I could turn <em>any</em> Markdown file into a tutorial exactly like this one.  So with surprisingly little work I modified the scripts to work with any markdown file and the html automatically generated from the standard Markdown.pl script.</p>
<p>I called this <a href="http://github.com/JosephPecoraro/markdownorial/tree/master">markdownorial</a>.  Laugh all you want at the name, but I still think the concept is very cool.  I&#8217;ll probably be using this more and more to automatically generate and format a pretty cool looking tutorial from a single markdown file.  The start to finish time for a project like this has instantly dropped to just the raw content part, no design or coding needed!</p>
<p>Advantages include:</p>
<ul>
<li>
<p>Writing Markdown is very fast and efficient.</p>
</li>
<li>
<p>Time is spent writing the content.  Not messing with design,</p>
</li>
<li>
<p>Table of Contents is automatically built for you.</p>
</li>
<li>
<p>Useful permalinks are automatically generated.  Very useful when passing around links.</p>
</li>
<li>
<p>Clean user interface that focuses entirely on the content but the Table of Contents is always available!</p>
</li>
<li>
<p>Git Repository means if I update the design its just a `git pull` away to get the update.</p>
</ul>
<p>Right now the tutorials shows up elegantly in all standards compliant browsers. Safari/Webkit and Chrome display it perfectly.  Opera has some very minor Unicode issues but displays everything perfectly. Firefox has some separate Unicode issues and if you don&#8217;t have the latest version it has some working but slow animation.  Overall, its entirely usable for people using decent browsers.</p>
<p>Let me know what you think.  Feel free to use it and improve it.  Its all up on Github.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2009/05/markdown-tutorial-in-1-step/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>First Nettuts Tutorial &#8211; .htaccess</title>
		<link>http://blog.bogojoker.com/2009/05/first-nettuts-tutorial-htaccess/</link>
		<comments>http://blog.bogojoker.com/2009/05/first-nettuts-tutorial-htaccess/#comments</comments>
		<pubDate>Mon, 11 May 2009 15:41:22 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[free]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[nettuts]]></category>

		<guid isPermaLink="false">http://blog.bogojoker.com/?p=776</guid>
		<description><![CDATA[My First Nettuts Tutorial was published today! For those who follow my blog every week, this is the reason why I haven&#8217;t been able to post the last few weeks&#8230; because I&#8217;ve been putting all the time I would normally be blogging into a series of Nettuts tutorials. This tutorial covers the basics of .htaccess, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://net.tutsplus.com/tutorials/other/the-ultimate-guide-to-htaccess-files/">My First Nettuts Tutorial</a> was published today!  For those who follow my blog every week, this is the reason why I haven&#8217;t been able to post the last few weeks&#8230; because I&#8217;ve been putting all the time I would normally be blogging into a series of Nettuts tutorials.</p>
<p><a href="http://net.tutsplus.com/tutorials/other/the-ultimate-guide-to-htaccess-files/"><br />
<img src="http://blog.bogojoker.com/wp-content/uploads/2009/05/picture-1.png" alt="htaccess examples" title="htaccess examples" width="555" height="329" style="border: 1px solid lightGray; margin-left: 10px;" /></a></p>
<p>This tutorial covers the basics of .htaccess, Apache&#8217;s Per Directory Configuration Files.  Fortunately, with the basics out of the way, I can move on to the cooler features such as GZip encoding and mod_rewrite for the next article.</p>
<p>Also the examples can be viewed here:<br />
<a href="http://bogojoker.com/htaccess/part1/">http://bogojoker.com/htaccess/part1/</a></p>
<p>And the examples can be downloaded here:<br />
<a href="http://bogojoker.com/htaccess/part1_examples.zip">http://bogojoker.com/htaccess/part1_examples.zip</a></p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2009/05/first-nettuts-tutorial-htaccess/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Ruby Readline Documentation</title>
		<link>http://blog.bogojoker.com/2009/03/ruby-readline-documentation/</link>
		<comments>http://blog.bogojoker.com/2009/03/ruby-readline-documentation/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 15:00:59 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[libedit]]></category>
		<category><![CDATA[readline]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://blog.bogojoker.com/?p=530</guid>
		<description><![CDATA[Before today the Ruby readline library lacked documentation! In order to find some decent documentation you would have to read the README packaged with the source. Michael Fellinger (manveru) from #ruby-lang offered to help me get some decent documentation online. Take a look!]]></description>
			<content:encoded><![CDATA[<p>Before today the Ruby readline library lacked documentation!  In order to find some decent documentation you would have to read the README packaged with the source.  Michael Fellinger (manveru) from <a href="irc://irc.freenode.net/#ruby-lang">#ruby-lang</a> offered to help me get some decent documentation online.  <a href="http://bogojoker.com/readline/">Take a look</a>!</p>
<p><a href="http://bogojoker.com/readline/"><img src="http://blog.bogojoker.com/wp-content/uploads/2009/03/readline.png" alt="readline" title="Ruby Readline Documentation" width="500" height="617" class="aligncenter size-full wp-image-531" style="border: 1px solid gray; padding: 0; margin: 20px 40px;" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2009/03/ruby-readline-documentation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Debugging HTTP Headers</title>
		<link>http://blog.bogojoker.com/2009/03/debugging-http-headers/</link>
		<comments>http://blog.bogojoker.com/2009/03/debugging-http-headers/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 15:30:11 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[headers]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[request]]></category>

		<guid isPermaLink="false">http://blog.bogojoker.com/?p=503</guid>
		<description><![CDATA[While writing the skreemr shell the other week I ran into an issue that required me to dig down to one of the lowest levels of web communication&#8230; HTTP Headers. I&#8217;m always shocked to learn that so many web developers don&#8217;t know much about headers. So I thought I would try to reenforce the point [...]]]></description>
			<content:encoded><![CDATA[<p>While writing <a href="http://blog.bogojoker.com/2009/02/mini-mp3-searching-shell-skreemr/">the skreemr shell</a> the other week I ran into an issue that required me to dig down to one of the lowest levels of web communication&#8230; HTTP Headers.  I&#8217;m always shocked to learn that so many web developers don&#8217;t know much about headers.  So I thought I would try to reenforce the point that HTTP Headers matter&#8230; and that knowing your stuff can help you debug and solve problems.  Here is the story of my real world example.</p>
<p>I&#8217;m not going to go over HTTP Headers.  <a href="http://www.cs.tut.fi/~jkorpela/http.html">thats</a> <a href="http://www.devshed.com/c/a/PHP/HTTP-Headers-in-Web-Development/">already</a> <a href="http://www.and.org/texts/server-http">been done</a>.  Instead I&#8217;m going to focus on debugging and working with them a little bit.   I&#8217;m going to assume you have already the general concepts.</p>
<h2>The Problem</h2>
<p>I wanted to add pagination to the skreemr shell.  So you could run a search, then get the next page of results, etc.  So a little experimentation with skreemr in my browser produced the following URLs:</p>
<pre>http://skreemr.com/results.jsp?q=test

http://skreemr.com/results.jsp?q=test&#038;l=10&#038;s=10

http://skreemr.com/results.jsp?q=test&#038;l=10&#038;s=20
</pre>
<p>A simple pattern!  &#8220;q&#8221; is the query string, &#8220;l&#8221; is the number per page, &#8220;s&#8221; is the number to start at, indexed from 0.  So that last URL would produce results 21-30.  This all worked well in my browsers, but it wasn&#8217;t working in Ruby:</p>
<pre class="textmate-source twilight"><span class="source source_ruby"><span class="meta meta_require meta_require_ruby"><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">require</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>open-uri<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span></span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Grab the pages and read the content
</span>str1 <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> open<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>http://skreemr.com/results.jsp?q=test<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>      <span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>read  <span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> 1-10
</span>str2 <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> open<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>http://skreemr.com/results.jsp?q=test&amp;s=10<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span> <span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>read  <span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> 11-20
</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span>=&gt; Should print false... but its printing true!
</span>puts str1<span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">==</span>str2</span></pre>
<p>What this was saying was that the content being returned from both of those urls is EXACTLY the same.  That couldn&#8217;t be&#8230; could it?</p>
<h2>More Investigation</h2>
<p>At this point I thought it was a problem in Ruby.  I figured Ruby was doing some caching in the background that I was going to have to disable or work around. (I now know this is not true, but that was my first guess).  To test that hypothesis I turned to my trusty friend curl and checked to see if that showed the proper behavior:</p>
<pre class="textmate-source twilight"><span class="source source_shell">shell<span class="keyword keyword_operator keyword_operator_redirect keyword_operator_redirect_shell">&gt;</span> curl <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>http://skreemr.com/results.jsp?q=test<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>      -o 1.html
shell<span class="keyword keyword_operator keyword_operator_redirect keyword_operator_redirect_shell">&gt;</span> curl <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>http://skreemr.com/results.jsp?q=test&amp;s=10<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span> -o 2.html
shell<span class="keyword keyword_operator keyword_operator_redirect keyword_operator_redirect_shell">&gt;</span> diff -q -s 1.html 2.html
Files 1.html and 2.html are identical
</span></pre>
<p>What!?!  That stunned me.  Curl was getting the exact same results as Ruby.  I took a look at the html files, and indeed 2.html, which should have contained results 11-20 held 1-10.  I opened both urls in my browser&#8230; they showed the correct results.  Something weird was happening!</p>
<h2>Take A Step Back</h2>
<p>At this point you&#8217;ve got to know what is happening.  Curl and Ruby (using Ruby&#8217;s Net:HTTP under the hood) are just making a simple GET request.  Both my browsers Safari and Firefox are sending far more then just a GET request.  They are sending a bunch of other headers.  Lets take a look at what Firebug says Firefox sent:</p>
<p><img src="http://blog.bogojoker.com/wp-content/uploads/2009/03/picture-1.png" alt="firebug" title="firebug" width="657" height="648" class="aligncenter size-full wp-image-507" style="border: none; margin-left: -40px;" /></p>
<p>Thats a mighty long list of headers!  Its entirely possible that one of those might be influencing the server.  Onto the drama!</p>
<h2>Time to Act &#8211; Literally</h2>
<p>The only difference between what curl sent in its request and what Firefox was sending is that header information and possibly any information contained in those cookies (unlikely in this case).  So lets make curl <em>act as if it is</em> Firefox by send the same headers with curl!  I took a quick peek at <a href="http://bogojoker.com/atom/curl.php">my curl reference</a> for the proper switches/formatting and I was ready:</p>
<pre class="textmate-source twilight"><span class="source source_shell">shell<span class="keyword keyword_operator keyword_operator_redirect keyword_operator_redirect_shell">&gt;</span> curl <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>http://skreemr.com/results.jsp?q=test<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span> -o 1.html
shell<span class="keyword keyword_operator keyword_operator_redirect keyword_operator_redirect_shell">&gt;</span> curl <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>http://skreemr.com/results.jsp?q=test&amp;s=10<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>            \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>Host: skreemr.com<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>                                  \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>User-Agent: Mozilla/5.0 (...) Firefox/3.0.6<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>        \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>Accept: text/html,application/xml;q=0.9,*/*;q=0.8<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>  \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>Accept-Language: en-us,en;q=0.5<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>                    \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>Accept-Encoding: gzip,deflate<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>                      \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>     \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>Keep-Alive: 300<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>                                    \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>Connection: keep-alive<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>                             \
            -H <span class="string string_quoted string_quoted_single string_quoted_single_shell"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_shell">'</span>Cache-Control: max-age=0<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_shell">'</span></span>                           \
            -o 2.html
shell<span class="keyword keyword_operator keyword_operator_redirect keyword_operator_redirect_shell">&gt;</span> diff -q -s 1.html 2.html
Files 1.html and 2.html differ
</span></pre>
<p>Jackpot!  Some subset of those headers is indeed fixing my problem, because now it properly fetched the second page of results!  Translating that back into Ruby works as well:</p>
<pre class="textmate-source twilight"><span class="source source_ruby"><span class="meta meta_require meta_require_ruby"><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">require</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>open-uri<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span></span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Headers
</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">HEADERS</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block">
</span>  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Host<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>            <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>skreemr.com<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>User-Agent<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>      <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Mozilla/5.0 (...) Firefox/3.0.6<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Accept<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>          <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>text/html,application/xml;q=0.9,*/*;q=0.8<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Accept-Language<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span> <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>en-us,en;q=0.5<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Accept-Encoding<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span> <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>gzip,deflate<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Accept-Charset<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>  <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>ISO-8859-1,utf-8;q=0.7,*;q=0.7<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Keep-Alive<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>      <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>300<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Connection<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>      <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>keep-alive<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span>
  <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Cache-Control<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>   <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>max-age=0<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span>
<span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Get the Pages
</span>str1 <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> open<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>http://skreemr.com/results.jsp?q=test<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">HEADERS</span> <span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>read
str2 <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> open<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>http://skreemr.com/results.jsp?q=test&amp;s=10<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="variable variable_other variable_other_constant variable_other_constant_ruby">HEADERS</span> <span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>read

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Correctly Prints false
</span>puts str1<span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">==</span>str2</span></pre>
<h2>Problem Solved</h2>
<p>Sure, we have a solution but its not pretty.  How much is really needed?  Finding that out is just a matter of eliminating the headers that do nothing.  Remove one, test, remove another, test, remove another test&#8230;  As long as it still works after you&#8217;ve removed an individual header you know that header wasn&#8217;t needed.  It took me a few minutes but I narrowed it down to just a single header:</p>
<pre class="textmate-source twilight"><span class="source source_ruby"><span class="variable variable_other variable_other_constant variable_other_constant_ruby">MAGIC</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block"> </span><span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>Accept-Language<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span> <span class="punctuation punctuation_separator punctuation_separator_key-value">=&gt;</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>en-us<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span>
</span></pre>
<p>This was a rather unique problem.  It is rare for me to have to dip down to the HTTP Headers to see what is actually happening for such a high level problem.  However, as this problem shows, its important to know what is going on under the hood.  If I didn&#8217;t know about headers, I would not have been able to solve it.<br />
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2009/03/debugging-http-headers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RDoc Introduction</title>
		<link>http://blog.bogojoker.com/2008/11/rdoc-introduction/</link>
		<comments>http://blog.bogojoker.com/2008/11/rdoc-introduction/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 05:05:43 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[rdoc]]></category>

		<guid isPermaLink="false">http://blog.bogojoker.com/?p=148</guid>
		<description><![CDATA[Automatically generating documentation from source code has been available as far back as 1993. Its so common now that its expected to be available in any mainstream programming languages. I&#8217;ve seen it most commonly in Object Oriented languages offering nicely formatted descriptions of classs and their public methods/attributes. Consistency is Nice The main advantage I [...]]]></description>
			<content:encoded><![CDATA[<p>Automatically generating documentation from source code has been available as far back as <a href="http://en.wikipedia.org/wiki/Comparison_of_documentation_generators">1993</a>.  Its so common now that its expected to be available in any mainstream programming languages.  I&#8217;ve seen it most commonly in Object Oriented languages offering nicely formatted descriptions of classs and their public methods/attributes.</p>
<h2>Consistency is Nice</h2>
<p>The main advantage I see with automatically generated documentation is that it is consistent.  Take Javadocs for instance.  They are all the same.  When a developer wants to work with a Java library, they expect Javadocs.  Why?  Because they are familiar with them.  They can easily navigate them and quickly find whatever it is that they are looking for.  Documentation in any other way would require wasting time learning how to use/navigate it searching for what you want to know.</p>
<p>RDoc is Ruby&#8217;s documentation generator.  You see RDoc generated documentation all over the place in Ruby.  See <a href="http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/index.html">YAML</a>,  <a href="http://code.whytheluckystiff.net/doc/hpricot/">Hpricot</a>, or even core classes like <a href="http://www.ruby-doc.org/core/classes/Array.html">Array</a>.</p>
<p>So, I felt if I want to continue using Ruby I should at least learn how its handled.  It turns out that its easier then I thought.  I&#8217;m a huge fan of Markdown syntax and RDoc turns out to be pretty close to that.  So, here is what I think is all you need to know to handle producing some simple, yet thorough, documentation for a class.</p>
<h2>RDoc Resources</h2>
<p>Start by updating your rdoc.  The latest version at the time of writing is 2.2.1.  The gem provides you with the rdoc and ri tools so that you can both generate and display documentation from the command line.  Here is how you can install them:</p>
<p><code>shell> sudo gem install rdoc</code></p>
<p>The best online resources I found were not surprisingly:</p>
<ul>
<li><a href="http://www.ruby-doc.org/stdlib/libdoc/rdoc/rdoc/index.html">RDoc&#8217;s documentation itself</a> &#8211; an RDoc on RDoc&#8230; actually its RDoc&#8217;s classes but its overview is helpful.</li>
<li><a href="http://rdoc.sourceforge.net/doc/files/README.html">An Older Writeup by Dave Thomas</a> &#8211; has a similar example</li>
<li><a href="http://daringfireball.net/projects/markdown/syntax">Markdown Syntax</a> &#8211; Very similar to what is used in RDoc comments</li>
</ul>
<p></p>
<h2>Structure</h2>
<p>Here is a basic example that shows the structure of the RDoc as it describes a File, Class, Attributes, and Methods.  The placement of the comments is important.  RDoc comments are always on top of what they are documenting:</p>
<pre class="textmate-source twilight"><span class="source source_ruby"><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Documentation for the file itself
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> There should be a blank line between this and any class
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> definition to separate the documentation about the file
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> and the class.  If there is no space then the entire text
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> is used for both the file and the class, no different.
</span>
<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Documentation for the class itself.
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> This will appear at the top of the page specific to this
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> class, before any other content.
</span><span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">Dice</span></span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Documentation for an attribute
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> To documentation each attribute you must make individual
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> calls to attr_accessor, attr_reader, and attr_writer.
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Appears next to the attribute name in the attrs section
</span>  <span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">attr_accessor</span> <span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>sides</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Documentation for the constructor
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Corresponds to the `new` method
</span>  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">initialize</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">sides</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    <span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>sides</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> sides
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Documentation for a method
</span>  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">roll</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">times</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    <span class="support support_class support_class_ruby">Array</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span>times<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>map <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block"> </span><span class="constant constant_numeric constant_numeric_ruby">1</span><span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">+</span>rand<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>sides</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span> <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Documentation for a method
</span>  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">beat</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">num</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    roll<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="constant constant_numeric constant_numeric_ruby">1</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>first <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">&gt;</span> num
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="keyword keyword_control keyword_control_ruby">end</span></span></pre>
<p>Running `rdoc` on <a href="http://bogojoker.com/blog/wp-content/uploads/2008/11/example1/">that file creates this documentation.</a></p>
<h2>Style</h2>
<p>Rich documentation makes the important parts stand out.  It makes use of HTML&#8217;s expressive power and enables lists, headers, links, bold/italics, code, and other presentation helpers.  I&#8217;ll now document the Dice class and add some style and realistic content.</p>
<pre class="textmate-source twilight"><span class="source source_ruby"><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"># == sample.rb
# This file contains the Dice class definition and it runs
# some simple test code on a 16 sided dice.  A 20 dice
# roll fight again the COMPUTER who always rolls 10s!

#
# Multi-sided dice class.  The number of sides is determined
# in the constructor, or later on by accessing the _sides_
# attribute.
#
# == Summary
#
# A #single_roll returns a single integer from 1 to the
# number of sides, _inclusive_.  However, if you want to
# roll multiple times you can can use the #roll method,
# specifying the number of rolls you want, and you will
# get an Array with the values of all the rolls!
#
# == Example
#
#    dice = Dice.new(8)   # An eight sided dice
#    four = dice.roll(4)  # An Array containing 4 rolls
#    sum  = four.inject(0) { |mem,i| mem+i } # Sum of rolls
#
# == Contact
#
# Author::  Joseph Pecoraro (mailto:joepeck02@gmail.com)
# Website:: http://blog.bogojoker.com
# Date::    Saturday November 29, 2008
#
</span><span class="meta meta_class meta_class_ruby"><span class="keyword keyword_control keyword_control_class keyword_control_class_ruby">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_ruby">Dice</span></span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Number of sides on the dice
</span>  <span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">attr_accessor</span> <span class="constant constant_other constant_other_symbol constant_other_symbol_ruby"><span class="punctuation punctuation_definition punctuation_definition_constant punctuation_definition_constant_ruby">:</span>sides</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Create a dice with `sides` of dice.
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Defaults to 6.
</span>  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">initialize</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">sides<span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span><span class="constant constant_numeric constant_numeric_ruby">6</span></span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    <span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>sides</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> sides
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Returns an array of size `times` containing
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> a group of dice rolls.
</span>  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">roll</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">times</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    <span class="support support_class support_class_ruby">Array</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span>times<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>map <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">{</span><span class="meta meta_syntax meta_syntax_ruby meta_syntax_ruby_start-block"> </span>single_roll <span class="punctuation punctuation_section punctuation_section_scope punctuation_section_scope_ruby">}</span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Returns the value of a single dice roll.  The
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> values are from 1 to @sides _inclusive_.
</span>  <span class="meta meta_function meta_function_method meta_function_method_without-arguments meta_function_method_without-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">single_roll</span></span>
    <span class="constant constant_numeric constant_numeric_ruby">1</span><span class="keyword keyword_operator keyword_operator_arithmetic keyword_operator_arithmetic_ruby">+</span>rand<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>sides</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> A single roll challenge:
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> * makes a single_roll
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> * returns true if the roll was strictly greater
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span>   then the given number
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby">  <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> * returns false otherwise
</span>  <span class="meta meta_function meta_function_method meta_function_method_with-arguments meta_function_method_with-arguments_ruby"><span class="keyword keyword_control keyword_control_def keyword_control_def_ruby">def</span> <span class="entity entity_name entity_name_function entity_name_function_ruby">beat</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">(</span><span class="variable variable_parameter variable_parameter_function variable_parameter_function_ruby">num</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_ruby">)</span></span>
    single_roll <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">&gt;</span> num
  <span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Note that this is a constant, which is special
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> and it is documented like a Class Attribute.
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> This is in the RDoc generated documentation for
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> the file.
</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">COMPUTER</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="constant constant_numeric constant_numeric_ruby">10</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Note that these comments, for generic code
</span><span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> are not in the RDoc generated documentation.
</span>dice <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="support support_class support_class_ruby">Dice</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span><span class="keyword keyword_other keyword_other_special-method keyword_other_special-method_ruby">new</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="constant constant_numeric constant_numeric_ruby">16</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>
winCount <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> loseCount <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="constant constant_numeric constant_numeric_ruby">0</span>
<span class="constant constant_numeric constant_numeric_ruby">20</span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>times <span class="keyword keyword_control keyword_control_start-block keyword_control_start-block_ruby">do
</span>  <span class="keyword keyword_control keyword_control_ruby">if</span> dice<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>beat<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="variable variable_other variable_other_constant variable_other_constant_ruby">COMPUTER</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span>
    winCount <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby">+=</span> <span class="constant constant_numeric constant_numeric_ruby">1</span>
  <span class="keyword keyword_control keyword_control_ruby">else</span>
    loseCount <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_augmented keyword_operator_assignment_augmented_ruby">+=</span> <span class="constant constant_numeric constant_numeric_ruby">1</span>
  <span class="keyword keyword_control keyword_control_ruby">end</span>
<span class="keyword keyword_control keyword_control_ruby">end</span>

<span class="comment comment_line comment_line_number-sign comment_line_number-sign_ruby"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_ruby">#</span> Output
</span>puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>You won <span class="source source_ruby source_ruby_embedded source_ruby_embedded_source"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">#{</span>winCount<span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">}</span></span> times and lost <span class="source source_ruby source_ruby_embedded source_ruby_embedded_source"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">#{</span>loseCount<span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">}</span></span> times!<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Muhahah.  Try again later!!<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>           <span class="keyword keyword_control keyword_control_ruby">if</span> winCount <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">&lt;</span> loseCount
puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>Well Played.  I'll get you next time.<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span> <span class="keyword keyword_control keyword_control_ruby">if</span> winCount <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">&gt;</span> loseCount
puts <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>What a match!  Boy that was fun.<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>      <span class="keyword keyword_control keyword_control_ruby">if</span> winCount <span class="keyword keyword_operator keyword_operator_comparison keyword_operator_comparison_ruby">==</span> loseCount</span></pre>
<p><a href="http://bogojoker.com/blog/wp-content/uploads/2008/11/example2/">That generates this documentation.</a></p>
<h2>Specifics</h2>
<p>There are some subtle points that make this documentation format nicely.  I&#8217;ll point them out and explain them.  Most of this is straight from the above resources, however some of it I could not find documented anywhere.</p>
<ul>
<li>The file documentation links to the Dice class.  Furthermore the Class documentation links down to the single_roll and roll methods.  This is because:<br />
<blockquote><p>Names of classes, source files, and any method names containing an underscore or preceded by a hash character are automatically hyperlinked from comment text to their description.</p></blockquote>
<ol>
<li>sample.rb was a filename and so it was automatically linked.</li>
<li>Dice was the name of a class and so it was automatically linked.</li>
<li>single_roll had an underscore and happened to be a method name so it was automatically linked in a few places.</li>
<li>#roll had a hash character signifying that it should be linked.</li>
</ol>
</li>
<li>Sections begin with a &#8220;=&#8221; or a &#8220;==&#8221;.  I prefer to use double, because it stands out more in the source code.  Technically a single &#8220;=&#8221; becomes a level 1 header, and a double becomes a level 2 header.  However, they both display the same.</li>
<li>URIs like http://blog.bogojoker.com and mailto:email are automatically turned into links and formatted nicely.</li>
<li>Bold, Italics, and Typewriter Text can be quickly formated much like Markdown:<br />
<blockquote><p>_italic_ or &lt;em>italic&lt;/em><br />
  *bold* or &lt;b>bold&lt;/b><br />
  +typewriter+ or &lt;tt>typewriter&lt;/tt></p></blockquote>
</li>
<li>Code is displayed if each line
<li>Tabular Labeled List, like the Contact information, are formatted like:<br />
<blockquote><p>label:: description 1<br />
label2:: both descriptions will line up</p></blockquote>
</li>
<li>Formatting source code is like Markdown.  The code that you want formatted must be indented with a few spaces.  As long as the indention is maintained the text will display as source code in the HTML documentation.</li>
<li>Formatting lists is again like Markdown.  Just use *&#8217;s or -&#8217;s and they will turn into bullet points.  For numbered lists just use numbers followed by a dot and they will be formatted automatically.</li>
</ul>
<p></p>
<h2>Final Notes on `rdoc` itself</h2>
<p>When I created the final documentation above I used a few of rdoc&#8217;s command line switches to customize the output.  What I actually used was:</p>
<p><code>shell> rdoc --title="Dice Documentation" --line-numbers --tab-width=2</code></p>
<p>The title switch changed the &lt;title> for the documentation page, and the other two deal with formatting the htmlized source code that RDoc shows when you click on the function name to view the source in the documentation.  There are plenty of command line switches.  To view the full list do:</p>
<p><code>shell> rdoc --help</code></p>
<p>A few useful switches are &#8220;&#8211;ri&#8221; to create ri documentation so you can access your classes from the command line.  Also you can output to several formats.  For instance you can make a PDF using &#8220;&#8211;format=texinfo&#8221; then using `texi2pdf` on the texinfo file.  The PDF doesn&#8217;t look that bad, here is <a href="http://bogojoker.com/blog/wp-content/uploads/2008/11/dice.pdf">my example as a PDF</a>.</p>
<p>NOTE: Finding the generators was tricky.  I had to check out the rdoc source code and find the different generators.  If anyone knows an easier way to check what generators are available, please let me know.</p>
<p>I hope this helps some people using RDoc for their classes.  Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2008/11/rdoc-introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AtomPub Overview and Curl Reference</title>
		<link>http://blog.bogojoker.com/2008/11/atompub-and-curl/</link>
		<comments>http://blog.bogojoker.com/2008/11/atompub-and-curl/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 07:22:07 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[atompub]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[reference]]></category>

		<guid isPermaLink="false">http://blog.bogojoker.com/?p=135</guid>
		<description><![CDATA[Not long ago I had to learn about the Atom Publishing Protocol for my job. I spent about a week learning on my own time all about XML, AtomPub, and even the basics of HTTP. After that week I decided to write down my own personal overview and example code to try and &#8220;visually&#8221; explain [...]]]></description>
			<content:encoded><![CDATA[<p>Not long ago I had to learn about the Atom Publishing Protocol for my job.  I spent about a week learning on my own time all about XML, AtomPub, and even the basics of HTTP.  After that week I decided to write down my own personal overview and example code to try and &#8220;visually&#8221; explain AtomPub as best I could.  The result was (and is):</p>
<p><a href="http://bogojoker.com/atom/">My Visual Guide to AtomPub</a></p>
<p>Now keep in mind that I wrote that only a few weeks after learning it.  The process of writing that guide forced myself to study it in greater detail than normal, actually run tests, and produce realistic output and examples.  I know its not perfect (I&#8217;d probably be slaughtered for my definition of REST) but over time I&#8217;ll be happy to improve and update it.  I think the design really improves the content making it readable, fun, and useful to refer to.
<p>I&#8217;m linking to it now because I&#8217;ve done a number of projects like this <a href="http://bogojoker.com/unix/">(my Unix Tutorial)</a> because I like sites that are strictly focused on one thing and do that one thing very well.  I&#8217;ll probably spend a little bit of time on remainder of my break from school by cleaning up these small &#8220;brain dump&#8221; websites.  I wanted to make sure they were mentioned and linked to from my blog.  Clearly they will be of no use to anyone if they are never linked to!</p>
<p>I decided to include a small `curl` reference on my AtomPub guide.  This is because its a very nice tool when working with HTTP requests and an overall generally useful shell program.  I think people might find the <a href="http://bogojoker.com/atom/curl.php">curl reference</a> useful.</p>
<p>I hope you enjoy this.  I&#8217;ll be linking to these occasionally as they grow.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2008/11/atompub-and-curl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Expression Examples</title>
		<link>http://blog.bogojoker.com/2008/01/regular-expression-examples/</link>
		<comments>http://blog.bogojoker.com/2008/01/regular-expression-examples/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 06:44:27 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://bogojoker.com/weblog/2008/01/23/regular-expression-examples/</guid>
		<description><![CDATA[A number of visitors have come to my website using the search terms regex replace. So I thought I would devote an entire article on how to use regular expressions to do a find and replace on a string in some popular languages. Example code is always attractive so lets get to the point! There [...]]]></description>
			<content:encoded><![CDATA[<p>A number of visitors have come to my website using the search terms <span class="green">regex replace</span>.  So I thought I would devote an entire article on how to use regular expressions to do a find and replace on a string in some popular languages.  Example code is always attractive so lets get to the point!  There is example code in Ruby, Perl, Python, Javascript, and Java.  [If you have other suggestions let me know or show me in your comments!]</p>
<p>All of the basic examples:</p>
<ol>
<li>put the string &#8220;one two three&#8221; into a variable</li>
<li>then use a regular expression and a native function to the language to</li>
<li>transform the original variable&#8217;s value to the new string &#8220;one 2 three&#8221;</li>
</ol>
<p>
<span style="cursor: pointer; text-decoration: underline; color: #D77;" onclick="elem = document.getElementById('basic'); elem.style.display = elem.style.display == 'none' ? '' : 'none';">Click Here For the Basic Examples</span>
</p>
<div id="basic" style="display: none;">
<p>Ruby: [my personal favorite]</p>
<pre class="textmate-source twilight"><span class="source source_ruby">str <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>one two three<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span>
str<span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>gsub!<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="string string_regexp string_regexp_classic string_regexp_classic_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_ruby">/</span>two<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_ruby">/</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="string string_quoted string_quoted_double string_quoted_double_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">"</span>2<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">"</span></span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span><span class="punctuation punctuation_separator punctuation_separator_statement punctuation_separator_statement_ruby">;</span></span></pre>
<p>Perl: [the most famous]</p>
<pre class="textmate-source twilight"><span class="source source_perl"><span class="variable variable_other variable_other_readwrite variable_other_readwrite_global variable_other_readwrite_global_perl"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_perl">$</span>str</span> = <span class="string string_quoted string_quoted_double string_quoted_double_perl"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_perl">"</span>one two three<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_perl">"</span></span>;
<span class="variable variable_other variable_other_readwrite variable_other_readwrite_global variable_other_readwrite_global_perl"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_perl">$</span>str</span> =~ <span class="string string_regexp string_regexp_replaceXXX string_regexp_replaceXXX_simple_delimiter string_regexp_replaceXXX_simple_delimiter_perl"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_perl"><span class="support support_function support_function_perl">s</span>/</span>two</span><span class="string string_regexp string_regexp_replaceXXX string_regexp_replaceXXX_format string_regexp_replaceXXX_format_simple_delimiter string_regexp_replaceXXX_format_simple_delimiter_perl"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_perl">/</span>2<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_perl">/</span></span>;</span></pre>
<p>Python: [for more scripters]</p>
<pre class="textmate-source twilight"><span class="source source_python"><span class="keyword keyword_control keyword_control_import keyword_control_import_python">import</span> re
<span class="support support_type support_type_python">str</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_python">=</span> <span class="string string_quoted string_quoted_double string_quoted_double_single-line string_quoted_double_single-line_python"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_python">"</span>one two three<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_python">"</span></span>
<span class="support support_type support_type_python">str</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_python">=</span> <span class="meta meta_function-call meta_function-call_python">re.sub<span class="punctuation punctuation_definition punctuation_definition_arguments punctuation_definition_arguments_begin punctuation_definition_arguments_begin_python">(</span><span class="meta meta_function-call meta_function-call_arguments meta_function-call_arguments_python"><span class="string string_quoted string_quoted_double string_quoted_double_single-line string_quoted_double_single-line_raw-regex string_quoted_double_single-line_raw-regex_python"><span class="storage storage_type storage_type_string storage_type_string_python">r</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_python">"</span>two<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_python">"</span></span>, <span class="string string_quoted string_quoted_double string_quoted_double_single-line string_quoted_double_single-line_python"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_python">"</span>2<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_python">"</span></span>, <span class="support support_type support_type_python">str</span></span><span class="punctuation punctuation_definition punctuation_definition_arguments punctuation_definition_arguments_end punctuation_definition_arguments_end_python">)</span></span></span></pre>
<p>Javascript: [for those web developers]</p>
<pre class="textmate-source twilight"><span class="source source_js">str <span class="keyword keyword_operator keyword_operator_js">=</span> <span class="string string_quoted string_quoted_double string_quoted_double_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">"</span>one two three<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">"</span></span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span>
str <span class="keyword keyword_operator keyword_operator_js">=</span> str<span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="support support_function support_function_js">replace</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_regexp string_regexp_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">/</span>two<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">/</span></span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">,</span> <span class="string string_quoted string_quoted_double string_quoted_double_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">"</span>2<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">"</span></span><span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span></span></pre>
<p>Java: [for students]</p>
<pre class="textmate-source twilight"><span class="source source_java"><span class="meta meta_class meta_class_java"><span class="storage storage_modifier storage_modifier_java">public</span> <span class="meta meta_class meta_class_identifier meta_class_identifier_java"><span class="storage storage_modifier storage_modifier_java">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_java">RegexTest</span></span> <span class="meta meta_class meta_class_body meta_class_body_java">{
  <span class="meta meta_method meta_method_java"><span class="storage storage_modifier storage_modifier_java">public</span> <span class="storage storage_modifier storage_modifier_java">static</span> <span class="meta meta_method meta_method_return-type meta_method_return-type_java"><span class="storage storage_type storage_type_primitive storage_type_primitive_array storage_type_primitive_array_java">void</span> </span><span class="meta meta_method meta_method_identifier meta_method_identifier_java"><span class="entity entity_name entity_name_function entity_name_function_java">main</span>(<span class="storage storage_type storage_type_object storage_type_object_array storage_type_object_array_java">String</span>[] <span class="variable variable_parameter variable_parameter_java">args</span>)</span> <span class="meta meta_method meta_method_body meta_method_body_java">{
    <span class="storage storage_type storage_type_java">String</span> str <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_java">=</span> <span class="string string_quoted string_quoted_double string_quoted_double_java"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_java">"</span>one two three<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_java">"</span></span><span class="punctuation punctuation_terminator punctuation_terminator_java">;</span>
    str <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_java">=</span> str<span class="keyword keyword_operator keyword_operator_dereference keyword_operator_dereference_java">.</span>replaceAll(<span class="string string_quoted string_quoted_double string_quoted_double_java"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_java">"</span>two<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_java">"</span></span>, <span class="string string_quoted string_quoted_double string_quoted_double_java"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_java">"</span>2<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_java">"</span></span>)<span class="punctuation punctuation_terminator punctuation_terminator_java">;</span>
  </span>}</span>
</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_end punctuation_section_class_end_java">}</span></span></span></pre>
<p>C++:</p>
<pre>If you have gotten to this point you may have done something wrong...</pre>
</div>
<p>Now you may recognize that in the above examples that regular expressions where not even needed.  All we did was find and replace a string and that simple task can be done without regular expressions!  So here is a more advanced example without the training wheels.</p>
<p>In the advanced examples:</p>
<ol>
<li>the string &#8220;a1b2c3&#8243; [may not need to be stored in a variable] is</li>
<li>manipulated by a [globally replacing] regular expression</li>
<li>resulting in &#8220;a11b22c33&#8243; [where all numbers, but not letters, are duplicated]</li>
<li>which is stored in a variable</li>
</ol>
<p>
<span style="cursor: pointer; text-decoration: underline; color: #D77;" onclick="elem = document.getElementById('advanced'); elem.style.display = elem.style.display == 'none' ? '' : 'none';">Click Here To Toggle the Advanced Examples</span>
</p>
<div id="advanced" style="display: block;">
<p>Ruby:</p>
<pre class="textmate-source twilight"><span class="source source_ruby">result <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_ruby">=</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>a1b2c3<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span><span class="punctuation punctuation_separator punctuation_separator_method punctuation_separator_method_ruby">.</span>gsub<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span> <span class="string string_regexp string_regexp_classic string_regexp_classic_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_ruby">/</span><span class="string string_regexp string_regexp_group string_regexp_group_ruby"><span class="punctuation punctuation_definition punctuation_definition_group punctuation_definition_group_ruby">(</span><span class="constant constant_character constant_character_escape constant_character_escape_ruby">\d</span><span class="punctuation punctuation_definition punctuation_definition_group punctuation_definition_group_ruby">)</span></span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_ruby">/</span></span><span class="punctuation punctuation_separator punctuation_separator_object punctuation_separator_object_ruby">,</span> <span class="string string_quoted string_quoted_single string_quoted_single_ruby"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_ruby">'</span>\1\1<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_ruby">'</span></span> <span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span></span></pre>
<p>Perl:</p>
<pre class="textmate-source twilight"><span class="source source_perl"><span class="variable variable_other variable_other_readwrite variable_other_readwrite_global variable_other_readwrite_global_perl"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_perl">$</span>result</span> = <span class="string string_quoted string_quoted_single string_quoted_single_perl"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_perl">'</span>a1b2c3<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_perl">'</span></span>;
<span class="variable variable_other variable_other_readwrite variable_other_readwrite_global variable_other_readwrite_global_perl"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_perl">$</span>result</span> =~ <span class="string string_regexp string_regexp_replaceXXX string_regexp_replaceXXX_simple_delimiter string_regexp_replaceXXX_simple_delimiter_perl"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_perl"><span class="support support_function support_function_perl">s</span>/</span>(<span class="constant constant_character constant_character_escape constant_character_escape_perl">\d</span>)</span><span class="string string_regexp string_regexp_replaceXXX string_regexp_replaceXXX_format string_regexp_replaceXXX_format_simple_delimiter string_regexp_replaceXXX_format_simple_delimiter_perl"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_perl">/</span><span class="constant constant_character constant_character_escape constant_character_escape_perl">\1\1</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_perl">/</span></span><span class="string string_regexp string_regexp_replace string_regexp_replace_perl"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_perl"><span class="keyword keyword_control keyword_control_regexp-option keyword_control_regexp-option_perl">g</span></span></span>;</span></pre>
<p>Python:</p>
<pre class="textmate-source twilight"><span class="source source_python"><span class="keyword keyword_control keyword_control_import keyword_control_import_python">import</span> re
result <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_python">=</span> <span class="meta meta_function-call meta_function-call_python">re.sub<span class="punctuation punctuation_definition punctuation_definition_arguments punctuation_definition_arguments_begin punctuation_definition_arguments_begin_python">(</span><span class="meta meta_function-call meta_function-call_arguments meta_function-call_arguments_python"><span class="string string_quoted string_quoted_single string_quoted_single_single-line string_quoted_single_single-line_raw-regex string_quoted_single_single-line_raw-regex_python"><span class="storage storage_type storage_type_string storage_type_string_python">r</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_python">'</span><span class="meta meta_group meta_group_regexp"><span class="punctuation punctuation_definition punctuation_definition_group punctuation_definition_group_regexp">(</span><span class="constant constant_character constant_character_character-class constant_character_character-class_regexp">\d</span><span class="punctuation punctuation_definition punctuation_definition_group punctuation_definition_group_regexp">)</span></span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_python">'</span></span>, <span class="string string_quoted string_quoted_single string_quoted_single_single-line string_quoted_single_single-line_python"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_python">'</span><span class="constant constant_character constant_character_escape constant_character_escape_backlash constant_character_escape_backlash_python">\\</span>1<span class="constant constant_character constant_character_escape constant_character_escape_backlash constant_character_escape_backlash_python">\\</span>1<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_python">'</span></span>, <span class="string string_quoted string_quoted_single string_quoted_single_single-line string_quoted_single_single-line_python"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_python">'</span>a1b2c3<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_python">'</span></span></span><span class="punctuation punctuation_definition punctuation_definition_arguments punctuation_definition_arguments_end punctuation_definition_arguments_end_python">)</span></span></span></pre>
<p>Javascript:</p>
<pre class="textmate-source twilight"><span class="source source_js"><span class="storage storage_type storage_type_js">var</span> result <span class="keyword keyword_operator keyword_operator_js">=</span> <span class="string string_quoted string_quoted_single string_quoted_single_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">'</span>a1b2c3<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">'</span></span><span class="meta meta_delimiter meta_delimiter_method meta_delimiter_method_period meta_delimiter_method_period_js">.</span><span class="support support_function support_function_js">replace</span><span class="meta meta_brace meta_brace_round meta_brace_round_js">(</span><span class="string string_regexp string_regexp_js"> <span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">/</span>(<span class="constant constant_character constant_character_escape constant_character_escape_js">\d</span>)<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">/</span>g</span><span class="meta meta_delimiter meta_delimiter_object meta_delimiter_object_comma meta_delimiter_object_comma_js">,</span> <span class="string string_quoted string_quoted_double string_quoted_double_js"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_js">"</span>$1$1<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_js">"</span></span> <span class="meta meta_brace meta_brace_round meta_brace_round_js">)</span><span class="punctuation punctuation_terminator punctuation_terminator_statement punctuation_terminator_statement_js">;</span></span></pre>
<p>Java:</p>
<pre class="textmate-source twilight"><span class="source source_java"><span class="meta meta_class meta_class_java"><span class="storage storage_modifier storage_modifier_java">public</span> <span class="meta meta_class meta_class_identifier meta_class_identifier_java"><span class="storage storage_modifier storage_modifier_java">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_java">RegexTest</span></span> <span class="meta meta_class meta_class_body meta_class_body_java">{
  <span class="meta meta_method meta_method_java"><span class="storage storage_modifier storage_modifier_java">public</span> <span class="storage storage_modifier storage_modifier_java">static</span> <span class="meta meta_method meta_method_return-type meta_method_return-type_java"><span class="storage storage_type storage_type_primitive storage_type_primitive_array storage_type_primitive_array_java">void</span> </span><span class="meta meta_method meta_method_identifier meta_method_identifier_java"><span class="entity entity_name entity_name_function entity_name_function_java">main</span>(<span class="storage storage_type storage_type_java">String</span> <span class="variable variable_parameter variable_parameter_java">args</span>[])</span> <span class="meta meta_method meta_method_body meta_method_body_java">{
    <span class="storage storage_type storage_type_java">String</span> str <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_java">=</span> <span class="string string_quoted string_quoted_double string_quoted_double_java"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_java">"</span>a1b2c3<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_java">"</span></span><span class="punctuation punctuation_terminator punctuation_terminator_java">;</span>
    <span class="storage storage_type storage_type_java">String</span> result <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_java">=</span> str<span class="keyword keyword_operator keyword_operator_dereference keyword_operator_dereference_java">.</span>replaceAll(<span class="string string_quoted string_quoted_double string_quoted_double_java"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_java">"</span>(<span class="constant constant_character constant_character_escape constant_character_escape_java">\\</span>d)<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_java">"</span></span>, <span class="string string_quoted string_quoted_double string_quoted_double_java"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_java">"</span>$1$1<span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_java">"</span></span>)<span class="punctuation punctuation_terminator punctuation_terminator_java">;</span>
  </span>}</span>
</span><span class="punctuation punctuation_section punctuation_section_class punctuation_section_class_end punctuation_section_class_end_java">}</span></span></span></pre>
<p>Pay strict attention to the number of backslashes required in python, the $1 used in Java and Javascript (however these are also global variables found in Ruby and Perl), and the trailing /g option required in Perl and Javascript for the global replacement.  Each language has its own little spin on things.
</p></div>
<p>I hope this helped answer your questions on regular expressions.  In case I whet your appetite on Regular Expressions I can point you to my <a href="http://bogojoker.com/weblog/2006/12/04/advance-your-programming-part-1-regex/">Introductory Article on Regular Expressions</a> and my <a href="http://bogojoker.com/weblog/2008/01/01/rr-11-in-place-edits-and-multiple-files/">command line utility rr</a> that allows you to run Ruby regular expression find and replace commands on files, standard input, and even piped input.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2008/01/regular-expression-examples/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Advance Your Programming &#8211; Part 2 &#8211; Templates</title>
		<link>http://blog.bogojoker.com/2007/05/advance-your-programming-part-2-templates/</link>
		<comments>http://blog.bogojoker.com/2007/05/advance-your-programming-part-2-templates/#comments</comments>
		<pubDate>Sun, 27 May 2007 16:40:13 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://bogojoker.com/weblog/2007/05/27/advance-your-programming-part-2-templates/</guid>
		<description><![CDATA[Every programmer is faced with code repetition at some point. Since the earliest programming in assembly language we have seen code repetition happens often and can be a problem. We realized that there should be a way to reduce the repetition to help the programmer. The most basic solution was creating a procedure (also known [...]]]></description>
			<content:encoded><![CDATA[<p>Every programmer is faced with code repetition at some point.  Since the earliest programming in assembly language we have seen code repetition happens often and can be a problem.  We realized that there should be a way to reduce the repetition to help the programmer.</p>
<p>The most basic solution was creating a procedure (also known as functions and methods), which would run the repetitive code.  Every point where where one would encounter a situation where he/she would need the code they would call the procedure (with parameters if necessary) and the procedure would run the code.  This had a number of advantages for the programmers.</p>
<ol>
<li>The amount of code could be greatly reduced.</li>
<li>The procedures/functions could be modular by using parameters</li>
<li>Changes would only need to be made in one place, greatly enhancing maintainability</li>
</ol>
<p>In terms of web development you will encounter programming situations in PHP and javascript where you will need to use the aforementioned method of creating procedures and functions but what about HTML/XHTML?</p>
<p>Repetition across HTML documents is obvious.  For starters, each document will have a head and a body.  Quite possibly there will be a footer within the body.  More then likely the header and footer for each document will be almost exactly the same.  First time programmers will see this as an excellent copy-and-paste opportunity but from an experienced perspective you can see that this is just code repetition.  The copy-and-paste technique will work but will almost surely cost you more work in the future.</p>
<p>Imagine this common situation, you have a website with 20 separate HTML documents each with the same CSS include files and the same footer &lt;div&gt; which displays contact information and links.  Something along the lines of a header:</p>
<pre>&lt;head&gt;
  &lt;title&gt;My Site - Homepage&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; src=&quot;general.css&quot; /&gt;
  &lt;script type=&quot;text/javascript&quot; src=&quot;scripts.js&quot; /&gt;
&lt;/head&gt;
</pre>
<p>And a footer:</p>
<pre>&lt;div id=&quot;footer&quot;&gt;
  &lt;p&gt;&amp;copy; 2006-2007 My Site&lt;/p&gt;
  &lt;p&gt;
     &lt;a href=&quot;index.html&quot;&gt;Home&lt;/a&gt; |
     &lt;a href=&quot;contact.html&quot;&gt;Contact&lt;/a&gt;
  &lt;/p&gt;
&lt;/div&gt;
</pre>
<p>Now imagine that you have to make a change.  You might want to change the name of the css or javascript file, add a javascript file to include, change the years in the footer or change one of the link locations or names.  To make any of these single changes you would have to go and manually edit all 20 pages.  This is the worst possible situation and it can be easily avoided.  The concept is using a template, and including repetitive code, which has the same three advantages above.</p>
<p>Templates are not possible solely with HTML.  That is unless you are using frames in which you have little to no control and horrible cross browser computability.  Don&#8217;t even try frames.  Template driven websites can be created very easily with server-side programming languages (PHP/ASP) or even just using a sophisticated web server like Apache using a concept called Service Side Includes (SSI).  The idea is that such a fundamental concept in both programming and design should be easy to implement, and templates are considered a major concept.</p>
<p>There are really only two steps needed to make this happen.  First put the section of html that you want included from all of your main documents into a single file.  Common extensions are .inc for include, but the extension does not matter.  The above case is a nice example, you could put the footer into its own file, footer.inc.  Many frameworks have an entire folder devoted to included files, often named includes/ in the root directory.  This is a good idea and makes it easy to use an absolute path inside any html document on the web server to the include files.</p>
<p>Finally you now need to include the file from you main html documents.  Here is the php way to include that file:</p>
<p>&lt;?php include &#8216;/includes/footer.inc&#8217;; ?&gt;</p>
<p>And the SSI method which looks a lot like an html comment (You may need to look into turning on SSI functionality on your webserver first, here is a resource):</p>
<p>&lt;!&#8211;#include virtual=&#8221;&#8216;/includes/footer.inc&#8217;&#8221; &#8211;&gt;</p>
<p>With the correct include statement above the contents of your include file will be placed at that position in the HTML document.  This is all done on the server, so the final product delivered to the client&#8217;s browser (the person visiting your website) will not be any different from the original.</p>
<p>There are differences between the PHP and SSI includes.  Without question the PHP includes are more powerful but I&#8217;ll give a brief description of each.  The SSI&#8217;s only go one level deep.  This means that if you include a file, you will not be able to include another file from within the included file.  SSI is meant to allow for very basic templating without a full blown server side programming language like PHP.</p>
<p>With PHP there are endless possibilities.  The included files can contain php code.  In actuality, when you include a file in PHP the contents of the file are run as if they are php code.  So any php tags will be properly executed.  Even the variables outside of the include are directly accessible because they are considered on the same scope as the included code.</p>
<p>There are a few ways you could put the above header a php include.  Here is a very straightforward approach.  First the PHP file to be include.  I will call it &#8216;header.php&#8217; because that is a clear indicator to me later that the file contains php code.  Notice the php tag and variable:</p>
<pre>&lt;head&gt;
  &lt;title&gt;My Site - &lt;?php echo $pageTitle; ?&gt;&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; src=&quot;general.css&quot; /&gt;
  &lt;script type=&quot;text/javascript&quot; src=&quot;scripts.js&quot; /&gt;
&lt;/head&gt;
</pre>
<p>The original php file that would set the variable before the include like so:</p>
<pre>&lt;?php
$pageTitle = &#039;Homepage&#039;;
include &#039;/includes/header.php&#039;;
?&gt;
</pre>
<p>This would work just fine.  It is a very loose form of templates.  The programmer is forced to keep track of the variables that are all floating around in the global scope.  Another approach would be to make a collection of functions, like an API.  Take this example:</p>
<pre>&lt;?php
/*
  Function to print out the &lt;head&gt; for each of my webpages.
  @param   $title   To go inside the &lt;title&gt;
*/
function printHead($title = &#039;My Site&#039;) {
?&gt;

&lt;head&gt;
  &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; src=&quot;general.css&quot; /&gt;
  &lt;script type=&quot;text/javascript&quot; src=&quot;scripts.js&quot; /&gt;
&lt;/head&gt;

&lt;?php
}
?&gt;
</pre>
<p>Clearly a little more structured approach.  You would have to include this file to make the function available, but everything from there is straightforward.  Just call the function and pass the appropriate title as a parameter to the function:</p>
<pre>&lt;?php
include &#039;/includes/commonFunctions.php&#039;;
printHead(&#039;My Site - Homepage&#039;);
?&gt;
</pre>
<p>Templates are a fundamental part of good programming.  Common uses are the header, footer, and menu&#8217;s for websites.  Very little variation is needed across a website for these portions, so consolidating all of the code into a single file is immensely helpful.  I just want to make sure that you learn about it soon and not like I did.  The hard way.</p>
<p><a href="http://httpd.apache.org/docs/2.0/howto/ssi.html">Introduction to Server Side Includes</a> &#8211; Apache&#8217;s very own HowTo.  A good start.</p>
<p><a href="http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/HTML/Q_22128326.html">Setup More SSI Setup Information</a> &#8211; Some Troubleshooting and Methods to get SSI working on Apache</p>
<p><a href="http://www.php.net/manual/en/function.include.php">PHP Include Function</a> &#8211; Documentation and Comments on the include function.  Will be helpful to PHP developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2007/05/advance-your-programming-part-2-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Unique Unix Tutorial</title>
		<link>http://blog.bogojoker.com/2007/04/a-unique-unix-tutorial/</link>
		<comments>http://blog.bogojoker.com/2007/04/a-unique-unix-tutorial/#comments</comments>
		<pubDate>Sun, 15 Apr 2007 03:47:18 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://bogojoker.com/weblog/2007/04/14/a-unique-unix-tutorial/</guid>
		<description><![CDATA[Thats right. You&#8217;ve all read tutorials. They range all over the place: boring, brief, detailed, useless, inspiring, the list goes on. There are those that are too technical, that you don&#8217;t understand until you look back at them at a later date. In the case of Unix and Linux the majority of the tutorials are [...]]]></description>
			<content:encoded><![CDATA[<p>Thats right.  You&#8217;ve all read tutorials.  They range all over the place: boring, brief, detailed, useless, inspiring, the list goes on.  There are those that are too technical, that you don&#8217;t understand until you look back at them at a later date.  In the case of Unix and Linux the majority of the tutorials are like that.  They are just reworded man pages.  Many people turn away from *nix because of this gap in technical familiarity.  My Unix tutorial aims to change this.</p>
<p>I have read a number of tutorials and I find the best are the ones that include you, the reader, in them.  You are participating in the tutorial.  Often there is a story, or some creative aspect that brings you into the tutorial.  You are learning in an entertaining way.  For first timers this can make all the difference.</p>
<p>My tutorial also gives tips and tricks for using the terminal.  They are the kind of tricks that make using the console much easier, but they are the tricks that you often don&#8217;t know unless someone shows you.  I hope that my tutorial at least gives insight to newer Unix/Linux users as they take the leap into a new realm.</p>
<p>Please take a look and offer your feedback to help me improve the tutorial.  I now present my <a href="http://bogojoker.com/unix/">Unix Tutorial</a>.</p>
<p>I would like to point out that the tutorial happened to be a college project that I recently decided to turn into a reality.  I have been and will be refurnishing the tutorial with improvements (sIFR), more rich content, while still maintaining my original goals and objectives.</p>
<p><span class="green">Inspiration</span>: A rather stimulating <a href="http://poignantguide.net/ruby/chapter-2.html">Ruby tutorial</a> that wraps you inside a rather creative story, with &#8220;synergy and cartoon foxes.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2007/04/a-unique-unix-tutorial/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Advance Your Programming &#8211; Part 1 &#8211; Regex</title>
		<link>http://blog.bogojoker.com/2006/12/advance-your-programming-part-1-regex/</link>
		<comments>http://blog.bogojoker.com/2006/12/advance-your-programming-part-1-regex/#comments</comments>
		<pubDate>Mon, 04 Dec 2006 05:05:27 +0000</pubDate>
		<dc:creator>Joseph Pecoraro</dc:creator>
				<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://bogojoker.com/weblog/2006/12/04/advance-your-programming-part-1-regex/</guid>
		<description><![CDATA[In my case I will talk about PHP, because it is so widely available (nearly 100% of reasonable hosting companies include PHP support in even their lowest plans). This article is also geared toward rather novice programmers because surely the aspects I mention can be seen on the tool belt of all master programmers. Regular [...]]]></description>
			<content:encoded><![CDATA[<p>In my case I will talk about PHP, because it is so widely available (nearly 100% of reasonable hosting companies include PHP support in even their lowest plans).  This article is also geared toward rather novice programmers because surely the aspects I mention can be seen on the tool belt of all master programmers.</p>
<ul>
<li>
<p>Regular Expressions</p>
</li>
<li>
<p>Templates and Includes</p>
</li>
<li>
<p>XML Parsing</p>
</li>
</ul>
<p>I will only touch light on these subjects in the hope that you will continue research on them if you have a desire to improve your skills.  Once learning each of these, your understanding of web programming will be greatly improved, your code may become cleaner and more concise, and most importantly you will have more fun programming!  Today&#8217;s article will focus on Regular Expressions, commonly refered to as regex.</p>
<p><strong>Regular Expressions &#8211; Regex</strong></p>
<p>This will not be an article teaching regular expressions (I may refer to them as &#8216;regex&#8217;).  I would rather this be a stimulus for you to learn regular expressions if you are currently in the dark, and afterwards I may provide you with a few sources to help you learn regular expressions.  You don&#8217;t need to really understand the regex that I show you in this article, nor will I really explain them to you.  You may even be able to decipher what the regex means on your own.  Keep in mind that I am showing you uses of regular expressions, their powerful and simplicity, as a means to entice you to learn them yourself.  You will not regret it.</p>
<p>Regular expressions are a language in their own.  At first glance, a regex may appear complex, but they are in fact very straightforward and they have many uses for web programmers.  Regex provide pattern matching for strings.  A common use is searching a string for something, if that something exists then execute some code.  Or the reverse, if the pattern is not found then execute some code.  One such example is input validation.</p>
<p>Lets say you have a simple contact form, which asks for a user&#8217;s phone number and email address.  Those are common form elements and should be checked for valid input so you can be sure users are not putting deliberately fake input or perhaps a simple typo in their phone number.  Regex can ensure that the users input is not only valid, but that it is also in the correct format that you specify, making your work easier.</p>
<p>Lets make a simple PHP example, with hardcoded values, and the phone number format we want is ###-###-####.</p>
<pre>

&lt;?php
$str = '555-555-555a';
$regex = '/^d{3}-d{3}-d{4}$/';
if (preg_match($regex, $str))
  echo 'This is a valid phone number.';
else
  echo 'Bad phone number.';
?&gt;
</pre>
<p>The regex is located between the forward slashes. / <em>here</em> /.  I am also using a PHP function preg_match( regex, string the regex works on ).  For those interested, here is the documentation on <a href="http://www.php.net/manual/en/function.preg-match.php">preg_match</a> from php.net. Copy, Paste, Save, and Run this example and you should see that the result is that 555-555-555a is indeed a bad phone number!  The reason, you probably determined, was that &#8216;a&#8217; is not a number!  The preg_match() function applies the regex on the string and finds that the string is not in the correct format (described above).  Powerful, simple, and fast!  To see the script in action with a working phone number change $str to any valid phone number, here is an example:</p>
<p>$str = &#8217;123-456-7890&#8242;;</p>
<p>With one simple string we force all input to be exactly how we want it!  To show how flexible regex can be, we can extend the regular expression to allow for a phone number in either: ###-###-### format, ######### format, or (###) ###-#### format!  All at the same time like so: /^(\d{3}-\d{3}-\d{4})|\d{10}|(\(\d{3}\) \d{3}-\d{4})$/</p>
<p>Now lets take a look at an email address.  An email address to be valid should look something like: example@domain.com.  It may even be as complex as: alpha.123@crazy.com, but lets just have a regex to allow for a simple email address.  Here goes:</p>
<pre>

&lt;?php
$email = 'example@domain.net';
if (preg_match('/^[a-z0-9_-]+@([_a-z0-9-]+.)+(com|net)$/i', $email))
  echo 'Valid Email ending in .com or .net';
else
  echo 'Bad email address!';
?&gt;
</pre>
<p>The regular expression allows for only a simple email address.  It only allows email address ending in .com or .net, but that can easily be extended.  I think you can see how powerful this is!  Only allow valid formatted email address will prevent problems down the line if you ever have to email this person using a script.  At least the email will be correctly formatted, at least your script won&#8217;t crash prematurely due to a formatting error, but instead the worst that could happen was the email is sent but no one ever receives it!</p>
<p>The simplicity of regex in form validation is unparalleled.  How would you ensure that the user put in a valid phone number?!?  Would you individually check each character in the string?  Make sure that the first character is a number, the second, the third, then a dash, number, number, &#8230;  A pain to do manually, regular expressions are just a single string which can do all of the work.</p>
<p>Soon I will provide another use of regular expression, search and replace!  There are countless uses but I will cover topics you will surely have to deal with as web programmers, and by learning regular expressions you will see a new world of possibilities.  It would be best if you started to learn the basics of regular expressions and so I will provide you with some links.</p>
<p><a href="http://immike.net/blog/2007/04/06/the-absolute-bare-minimum-every-programmer-should-know-about-regular-expressions/">Overview</a> &#8211; A Brief but Very Accurate Overview of Regular Expressions.</p>
<p><a href="http://www.regular-expressions.info/">Regular-Expressions.info</a> &#8211; Regex Tutorial, Examples and Reference &#8211; Regexp Patterns<br />
This is the website I highly recommend that you start at.</p>
<p><a href="http://en.wikipedia.org/wiki/Regular_expression">Regular expression</a> &#8211; Wikipedia, the free encyclopedia<br />
Wikipedia is always a great resource for programming questions.</p>
<p><a href="http://www.ilovejackdaniels.com/cheat-sheets/regular-expressions-cheat-sheet/">Regular Expressions Cheat Sheet</a> &#8211; Cheat Sheets &#8211; ILoveJackDaniels.com<br />
If ever you need a cheat sheet, be it PHP, Ruby on Rails, or CSS, get it here!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.bogojoker.com/2006/12/advance-your-programming-part-1-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

