<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Man With Code &#187; data</title>
	<atom:link href="http://manwithcode.com/tag/data/feed/" rel="self" type="application/rss+xml" />
	<link>http://manwithcode.com</link>
	<description>Teaching You, One Tutorial at a Time</description>
	<lastBuildDate>Thu, 09 Feb 2012 03:57:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Programming With Ruby Episode 14, YAML</title>
		<link>http://manwithcode.com/205/programming-with-ruby-episode-14-yaml/</link>
		<comments>http://manwithcode.com/205/programming-with-ruby-episode-14-yaml/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 08:14:03 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Ruby Programming]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[storing]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://manwithcode.com/?p=205</guid>
		<description><![CDATA[Covered in this Episode: What is YAML Why should you use YAML? Storing Data Transcript: Hello Everybody and welcome to Programming With Ruby Episode 14, YAML. I&#8217;m Tyler and this video is brought to you by manwithcode.com. I will start off this episode telling you what YAML is and why you should be using it. [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/NSifr3DflxQ&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/NSifr3DflxQ&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><strong>Covered in this Episode:</strong></p>
<ul>
<li>What is YAML</li>
<li>Why should you use YAML?</li>
<li>Storing Data</li>
</ul>
<p><strong>Transcript:<br />
</strong></p>
<p>Hello Everybody and welcome to Programming With Ruby Episode 14,<br />
YAML. I&#8217;m Tyler and this video is brought to you by manwithcode.com.</p>
<p>I will start off this episode telling you what YAML is and why you<br />
should be using it. I will then be showing you how you can store<br />
objects and various assorted other kinds of data using YAML.</p>
<p>Without further delay, lets get started!</p>
<p><strong>What is YAML?</strong></p>
<p>YAML stands for YAML Ain&#8217;t Markup Language. In the most basic sense it<br />
is a way of storing data for later use.</p>
<p><strong>Why should you use YAML?</strong></p>
<p>Lets say you are writing a game, you could save your players progress<br />
to a YAML file. Or if your writing an application with many preference<br />
settings, you might save the user&#8217;s preferences to a YAML file.</p>
<p>Using YAML instead of creating your own text-based format creates more<br />
portability, because other applications can immediately use your YAML<br />
file instead of having to write their own custom file loader.</p>
<p>YAML also makes editing files by hand very easy to do if you have to.</p>
<p>Enough of all the theoretical talk, lets write some code!</p>
<p><strong>Storing Data</strong></p>
<p>Lets say we are writing a text editor and there are a few preferences the<br />
user can change. We want the user to be able to set their preferences and<br />
have them saved, so the next time they start up our text editor the<br />
preferences are set the same as they were at last use.</p>
<p>We decide to use YAML to store our users preferences. Lets say for<br />
example that the preferences are stored in a hash:</p>
<pre class="brush: ruby; title: ; notranslate">
@preferences = {&quot;word-wrapping&quot; =&gt; true, &quot;font-size&quot; =&gt; 20, &quot;font&quot; =&gt; &quot;Arial&quot;}
</pre>
<p>To save that using YAML we have to use:</p>
<pre class="brush: ruby; title: ; notranslate">
require 'yaml'
</pre>
<p>Open a file for writing (YAML files end in .yml or .yaml):</p>
<pre class="brush: ruby; title: ; notranslate">
output = File.new('prefs.yml', 'w')
</pre>
<p>Use the YAML.dump method to get the text that will be outputted:</p>
<pre class="brush: ruby; title: ; notranslate">
output.puts YAML.dump(@preferences)
output.close
</pre>
<p>Full source:</p>
<pre class="brush: ruby; title: ; notranslate">
require 'yaml'
@preferences = {&quot;word-wrapping&quot; =&gt; true, &quot;font-size&quot; =&gt; 20, &quot;font&quot; =&gt; &quot;Arial&quot;}

output = File.new('prefs.yml', 'w')
output.puts YAML.dump(@preferences)
output.close
</pre>
<p>And that is how easy it is to store data!</p>
<p>We read back a file in a similar way, using the YAML.load method:</p>
<pre class="brush: ruby; title: ; notranslate">
require 'yaml'

output = File.new('prefs.yml', 'r')
@preferences = YAML.load(output.read)
output.close
</pre>
<p>Don&#8217;t think you&#8217;re limited to simple data like hashes and arrays, you<br />
can store objects too!</p>
<p>It has been my experience that it is usually best to store data in<br />
hashes, because they automatically have a label, unlike an array. You<br />
can do whatever you want to, but that is my recommendation.</p>
<p>Now comes the sad time when we have to close the episode.</p>
<p>Please don&#8217;t forget to donate, a few dollars can go a long way.</p>
<p>If you have any questions, comments or suggestions, please do not<br />
hesitate to leave a comment below or email me at tyler@manwithcode.com</p>
<p>Thank for watching!</p>
<p>Bye!</p>
]]></content:encoded>
			<wfw:commentRss>http://manwithcode.com/205/programming-with-ruby-episode-14-yaml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Ruby Programming]]></series:name>
	</item>
	</channel>
</rss>

