<?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>Blog - ShiftEleven &#187; Ruby off Rails</title>
	<atom:link href="http://shifteleven.com/articles/category/ruby-off-rails/feed" rel="self" type="application/rss+xml" />
	<link>http://shifteleven.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 19 Sep 2009 11:19:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-alpha</generator>
		<item>
		<title>Useless Ruby Tricks: DATA and __END__</title>
		<link>http://shifteleven.com/articles/2009/02/09/useless-ruby-tricks-data-and-__end__</link>
		<comments>http://shifteleven.com/articles/2009/02/09/useless-ruby-tricks-data-and-__end__#comments</comments>
		<pubDate>Tue, 10 Feb 2009 03:20:17 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby off Rails]]></category>
		<category><![CDATA[DATA]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[__END__]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=207</guid>
		<description><![CDATA[So maybe not totally useless certainly fun.  Normally, ruby scripts are finished when you reach the end of a file; however, this is not always the case.  You can end your script sooner by using the __END__ keyword in your script.  Once added, everything you type after that will not be parsed [...]]]></description>
			<content:encoded><![CDATA[<p>So maybe not totally useless certainly fun.  Normally, ruby scripts are finished when you reach the end of a file; however, this is not always the case.  You can end your script sooner by using the <code>__END__</code> keyword in your script.  Once added, everything you type after that will not be parsed by ruby.</p>
<p>So what?</p>
<p>Well, you can use the global variable <code>DATA</code> to get the contents of what you  wrote after the <code>__END__</code> block.  <code>DATA</code> is actually a <code>File</code> object to just that piece of text in your script.</p>
<p><span id="more-207"></span></p>
<p>How about a little sample to make things a little clearer?</p>
<pre class="ruby" title="code">#!/usr/bin/env ruby
%w(yaml pp).each { |dep| require dep }

obj = YAML::load(DATA)

pp obj

__END__
---
-
  name: Adam
  age: 28
  admin: true
-
  name: Maggie
  age: 28
  admin: false</pre>
<p>So with this, I was able to embed a little bit of YAML directly into my script.  I added the YAML after <code>__END__</code>.  <code>YAML::load</code> will accept a <code>File</code> object, so I just passed it <code>DATA</code> and now I have a reconstituted array.</p>
<p>Maybe that&#8217;s not the most practical use of this information; however, <a href="http://www.sinatrarb.com/">Sinatra</a> provides a really awesome use of this.  With Sinatra, you can create a whole web application that lives in one single, solitary ruby file.  Sinatra can use in-file templates.  In-file templates are added into the space after <code>__END__</code> where each view file is annotated with <code>@@view_name</code>.</p>
<pre class="ruby" title="code">require 'rubygems'
require 'sinatra'

get '/' do
  haml :index
end

__END__

@@ layout
%html
  = yield

@@ index
%div.title Hello world!!!!!</pre>
<p>Not bad for a useless trick!</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2009/02/09/useless-ruby-tricks-data-and-__end__/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Converting ERB to HAML snippet</title>
		<link>http://shifteleven.com/articles/2008/06/08/converting-erb-to-haml-snippet</link>
		<comments>http://shifteleven.com/articles/2008/06/08/converting-erb-to-haml-snippet#comments</comments>
		<pubDate>Sun, 08 Jun 2008 22:45:39 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby off Rails]]></category>
		<category><![CDATA[erb]]></category>
		<category><![CDATA[haml]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=80</guid>
		<description><![CDATA[I&#8217;m playing around with merb so I&#8217;m using merb-gen to create some basic scaffolding; however, I&#8217;m not using ERB, the default rendering engine, I want to use haml
Haml comes with a script, html2haml, which can take HTML with ERB and convert it nicely to haml.  Seeing as I would have to run that command [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m playing around with <a href="http://merbivore.com/features.html,">merb</a> so I&#8217;m using <code>merb-gen</code> to create some basic scaffolding; however, I&#8217;m not using ERB, the default rendering engine, I want to use <a href="http://haml.hamptoncatlin.com/.">haml</a></p>
<p>Haml comes with a script, <code>html2haml</code>, which can take HTML with ERB and convert it nicely to haml.  Seeing as I would have to run that command more than once, I have a little command snippet that I like to use to aid with the conversion.</p>
<pre class="bash" title="code">$ find . -name '*erb' | \
xargs ruby -e 'ARGV.each { |i| puts "html2haml -r #{i} #{i.sub(/erb$/,"haml")}"}'</pre>
<p>That will print out what the <code>html2haml</code> commands would be, it does *not* run the command.  I do that step such that I can review what will be converted.  If I like what&#8217;s going to be done, then I just run that command and pipe it into bash&#8230;like so</p>
<pre class="bash" title="code">$ find . -name '*erb' | \
xargs ruby -e 'ARGV.each { |i| puts "html2haml -r #{i} #{i.sub(/erb$/,"haml")}"}' | \
bash</pre>
<p>If you like it, it probably wouldn&#8217;t hurt storing that as a shell script somewhere :)</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/06/08/converting-erb-to-haml-snippet/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Forget About Little Old CGI</title>
		<link>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi</link>
		<comments>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi#comments</comments>
		<pubDate>Tue, 18 Mar 2008 00:53:31 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby off Rails]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=41</guid>
		<description><![CDATA[Maybe someone may have told you to use ruby and CGI together, but I think that person has a screw loose.  I mean, come on.  Everyone knows that what you really need to use is Rack

Why Rack
Well for one, it&#8217;s light weight.  So if you want to deploy your ruby script using [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe someone may have told you to use <a href="http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi">ruby and CGI</a> together, but I think that person has a screw loose.  I mean, come on.  Everyone knows that what you really need to use is <a href="http://rack.rubyforge.org/">Rack</a></p>
<p><span id="more-41"></span></p>
<h3>Why Rack</h3>
<p>Well for one, it&#8217;s light weight.  So if you want to deploy your ruby script using CGI, you can do that with Rack.</p>
<p>But let&#8217;s say, you want to your script to Mongrel, or WEBRick, or FCGI; you can do that too, and with very few changes to your script.  That&#8217;s because Rack abstracts all the boilerplate code you would need to work with those servers and bundles it up into a nice interface for you to use.</p>
<p>Oh, and the code to use Rack is very easy.  Here&#8217;s an example of a Rack app for you:</p>
<h3>Examples</h3>
<pre class="ruby" title="code">%w(rubygems rack haml).each { |dep| require dep }

class IndexPage
  def call(env)
    haml_engine = Haml::Engine.new(File.read('../templates/index.haml'))
    [
      200,
      { "Content-Type" =&gt; "text/html" },
      haml_engine.render(Object.new, {})
    ]
  end
end

Rack::Handler::CGI.run(IndexPage.new)</pre>
<p>The secret behind Rack is that it will run any ruby object that has a <code>call</code> method and pass in the environment variables to it.  In that environment variable, you will find things like HTTP_HOST, or QUERY_STRING, and the like.</p>
<p>If you wanted to have this script now work with WEBRick, no problem.</p>
<pre class="ruby" title="code"># Same code as above, with just a little tweak to the last line

Rack::Handler::WEBRick.run(IndexPage.new, :Port =&gt; 3000)</pre>
<p>Tada!  So now you can run a test server without worrying about setting up Apache or Lighttpd.</p>
<h3>More Goodness</h3>
<p>To make your life easier, Rack also comes with a set of middleware for you to use.  Say you have an error in your application.  Perhaps you would like to see that error formatted nicely in your browser.  You can enable <code>Rack::ShowExceptions</code>.  Perhaps you would like the code you have to be reloaded from the server whenever it changes, like when you&#8217;re developing your application, then you can use <code>Rack::Reloader</code>.</p>
<p>To use those options, you can write your Rack handler like</p>
<pre class="ruby" title="code">app = Rack::Builder.new do
  use Rack::CommonLogger  # apache-like logger
  use Rack::ShowExceptions
  use Rack::Reloader
  use Rack::ShowStatus  # create default pages for common HTTP error codes
  use Rack::Lint  #ensures all applications behave validly
  run Index.new
end
Rack::Handler::WEBRick.run(app, :Port =&gt; 3000)</pre>
<p>There&#8217;s plenty of magic going on with Rack; A magic that does not sacrifice speed and/or flexibility.  I would highly recommend giving Rack a look into the next time you want to write a simple web page.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/03/17/forget-about-little-old-cgi/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Forget About Little Old CGI</title>
		<link>http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi</link>
		<comments>http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi#comments</comments>
		<pubDate>Fri, 14 Mar 2008 03:36:46 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[Ruby off Rails]]></category>
		<category><![CDATA[cgi]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://shifteleven.com/?p=40</guid>
		<description><![CDATA[Rails is great and all, but sometimes it can be just a little too much and you just need to set up a few pages, not the next big app.  Fear not, there is still tool in the ruby tool box at your disposal: the CGI library CGI is fast and lean and still [...]]]></description>
			<content:encoded><![CDATA[<p>Rails is great and all, but sometimes it can be just a little too much and you just need to set up a few pages, not the next big app.  Fear not, there is still tool in the ruby tool box at your disposal: <a href="http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html.">the CGI library</a> CGI is fast and lean and still can be used will all of your favorite friends, like <a href="http://haml.hamptoncatlin.com/">HAML</a></p>
<p><span id="more-40"></span></p>
<p>Since you&#8217;re using CGI, you don&#8217;t _have_ to worry about setting up FCGI, or Mongrel.  You can use <a href="http://www.modruby.net/en/">mod_ruby</a> if you&#8217;re using Apache or cgi with Lighttpd.</p>
<p>So now that servers are out of the way, time for some code.</p>
<p>I mentioned that you don&#8217;t have to give up HAML. So for that, I propose this little directory structure.</p>
<pre title="code">mysite/
   |- templates/
   `- public/</pre>
<p>Hopefully you guessed that <code>templates</code> contains your HAML templates and <code>public</code> is the webroot for your site.  Now in your <code>public</code> directory, create an <code>index.rb</code> file, and perhaps it could somewhat look like:</p>
<pre class="ruby" title="code">require 'rubygems'
require 'haml'
require 'cgi'

# A Hash for all of the variables we want to send to HAML
model = {}

# Why not get the time
model[:time] = Time.now

cgi = CGI.new

# Set up the template
haml_engine = Haml::Engine.new(File.read('../templates/index.haml'))

# Render out the HTML
cgi.out { haml_engine.render(Object.new, model) }</pre>
<p>And there&#8217;s the tedious code.  There are some interesting pieces to notice.  For one, when calling cgi.out, that sends out the proper headers out to the browser *(very important)*.  Also, this could probably be wrapped up into a nice function to reduce the strain of repeating one&#8217;s self.</p>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2008/03/13/dont-forget-about-little-old-cgi/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
