<?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; input</title>
	<atom:link href="http://manwithcode.com/tag/input/feed/" rel="self" type="application/rss+xml" />
	<link>http://manwithcode.com</link>
	<description>Teaching You, One Tutorial at a Time</description>
	<lastBuildDate>Tue, 22 Feb 2011 23:40:35 +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 13, Basic I/O</title>
		<link>http://manwithcode.com/197/programming-with-ruby-episode-13-basic-io/</link>
		<comments>http://manwithcode.com/197/programming-with-ruby-episode-13-basic-io/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 08:07:03 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Ruby Programming]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[directories]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[io]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://manwithcode.com/?p=197</guid>
		<description><![CDATA[Covered in this Episode: Defining I/O Files Directories Transcript: Hello Everybody and welcome to Programming With Ruby Episode 13, Basic input output. As always, I&#8217;m Tyler and this video is brought to you by manwithcode.com In this episode I will be defining what exactly input/output is, You will also learn how to work with files [...]]]></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/x-ru_Hw7YNI&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/x-ru_Hw7YNI&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><strong>Covered in this Episode:</strong></p>
<ul>
<li>Defining I/O</li>
<li>Files</li>
<li>Directories</li>
</ul>
<p><strong>Transcript:</strong></p>
<p>Hello Everybody and welcome to Programming With Ruby Episode 13, Basic<br />
input output. As always, I&#8217;m Tyler and this video is brought to you by<br />
manwithcode.com</p>
<p>In this episode I will be defining what exactly input/output is, You<br />
will also learn how to work with files and directories in Ruby</p>
<p>Lets get started!</p>
<p><strong>Defining I/O</strong></p>
<p>So what exactly is I/O or input/output. Basically Input is any data that<br />
you receive from the user, and output is what data you give to the<br />
user, usually based on what input the user gave you.</p>
<p>In this episode you will be seeing I/O as working with files and<br />
directories. You have already worked with I/O previously when we<br />
talked about getting strings from the user (gets) and printing string<br />
to the screen (puts)</p>
<p><strong>Files</strong></p>
<p>There are a couple of ways to start working with files. The first is<br />
by using File.new:</p>
<pre class="brush: ruby; title: ; notranslate">
my_file = File.new('file', 'r')
my_file.close
</pre>
<p>File.new opens the file (the first parameter), for the specified mode<br />
(the second parameter)</p>
<p>The file name you use can be either just the relative file name<br />
(notes.txt) or the full path to the file (C:\Documents and<br />
Settings\Tyler\My Documents\notes.txt). Just remember that if you use<br />
only the file name, that file must be in the same folder your program<br />
is located in!</p>
<p>The mode defines how you are going to use the file you&#8217;ve opened. In<br />
the example &#8216;r&#8217; is used. Which stands for read, meaning we are only<br />
going to read from the file.</p>
<p>&#8216;w&#8217; is write meaning we are writing to the file specified, be careful<br />
when using write mode, as the whole file is cleared when it is opened.</p>
<p>&#8216;a&#8217; is append, which is similar to write, except instead of clearing<br />
the whole file, it appends all written data to the end.</p>
<p>All the different modes can have &#8216;+&#8217; at the end of them, which opens<br />
up both read and write functionality. (ex: &#8216;w+&#8217;)</p>
<p>Now you know how to open a file, how exactly do yo go about writing to<br />
it? By simply using puts and print, like you did when outputting data<br />
to the screen, except they output data to the file!</p>
<pre class="brush: ruby; title: ; notranslate">
my_file = File.new('example.txt', 'w')
my_file.puts &quot;Hello World!&quot;
my_file.close
</pre>
<p>If you want to read from a file you can use File.read:</p>
<pre class="brush: ruby; title: ; notranslate">
my_file = File.new('example.txt', 'r')
puts my_file.read
my_file.close
</pre>
<p>You can also pass in how many characters you want to read as a parameter.</p>
<p>If you would rather have the lines of a file in an array, you can use<br />
file.readlines.</p>
<p>If you are unsure a file exists you can simply use File.exists?:</p>
<pre class="brush: ruby; title: ; notranslate">
File.exists(&quot;notes.txt&quot;)
</pre>
<p>Now it is important to note that every time I am done using a file I<br />
call the .close method, why do I do this? Because if you don&#8217;t close<br />
your access on the file, it can become unusable by other<br />
programs. Also, your computers operating system might not write the<br />
changes you&#8217;ve made to the file, until your program has released its<br />
hold on it.</p>
<p>As an alternative to using file.close every single time, we can use<br />
File.open, instead of File.new. File.open lets us use the file in a<br />
code block, at the end of the code block, the file is closed<br />
automatically.</p>
<pre class="brush: ruby; title: ; notranslate">
File.open('hello.txt', 'w') do |file|
    file.puts &quot;Hello World!&quot;
end
</pre>
<p>There is a whole lot more you can do with files in Ruby to learn more<br />
go to http://ruby-doc.org/core-1.8.7/index.html and find the File and IO class</p>
<p><strong>Directories</strong></p>
<p>You can work with directories (also known as folders) in Ruby using<br />
Dir. You can create directories with Dir.mkdir:</p>
<pre class="brush: ruby; title: ; notranslate">
Dir.mkdir(&quot;Hello&quot;)
</pre>
<p>You can get an array of all the files and directories that are in the<br />
current directory with Dir.entries:</p>
<pre class="brush: ruby; title: ; notranslate">
Dir.entries('.')
</pre>
<p>Keep in mind that . is your current directory, and .. is the directory<br />
one level above the current one. Also, like files you can specify the<br />
relative path or the full path.</p>
<p>There is also another method similar to Dir.entries, Dir.foreach. They<br />
are essentially the same, except foreach allows you to iterate over<br />
each item in a code block.</p>
<pre class="brush: ruby; title: ; notranslate">
Dir.foreach('.') do |entry|
    puts entry
end
</pre>
<p>Like Files, there is a lot of material I couldn&#8217;t cover to learn more<br />
go to http://ruby-doc.org/core-1.8.7/index.html and find the Dir class</p>
<p>This brings us to the end of the episode.</p>
<p>If you like these videos please donate.</p>
<p>And if you have any Questions, Comments or Suggestions, you can leave<br />
a comment in the comment box below, or email me at<br />
tyler@manwithcode.com</p>
<p>Thank you very much for watching, goodbye.</p>
]]></content:encoded>
			<wfw:commentRss>http://manwithcode.com/197/programming-with-ruby-episode-13-basic-io/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Ruby Programming]]></series:name>
	</item>
	</channel>
</rss>

