<?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; DATA</title>
	<atom:link href="http://shifteleven.com/articles/tag/data/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>
	</channel>
</rss>
