<?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; classes</title>
	<atom:link href="http://manwithcode.com/tag/classes/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 4, Main Ruby Concepts</title>
		<link>http://manwithcode.com/84/programming-with-ruby-episode-4-main-ruby-concepts/</link>
		<comments>http://manwithcode.com/84/programming-with-ruby-episode-4-main-ruby-concepts/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 08:54:51 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Ruby Programming]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[concepts]]></category>
		<category><![CDATA[main]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://manwithcode.com/?p=84</guid>
		<description><![CDATA[Covered In This Episode: Basic Variables Basic Methods Basic Classes Interactive Ruby (irb) Code: Transcript: Hello everybody and welcome to Programming With Ruby Episode 4, Main Ruby Concepts. Covered in this episode. We&#8217;ll be playing with interactive ruby (also called irb). I will teach you about variables, basic methods, and classes in Ruby. Lets get [...]]]></description>
			<content:encoded><![CDATA[<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/8W7MJrAzeWw&#038;hl=en&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/8W7MJrAzeWw&#038;hl=en&#038;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object><br />
<strong>Covered In This Episode:</strong></p>
<ul>
<li>Basic Variables</li>
<li>Basic Methods</li>
<li>Basic Classes</li>
<li>Interactive Ruby (irb)</li>
</ul>
<p><strong>Code:</strong></p>
<pre class="brush: ruby; title: ; notranslate">
# Define the class Greeter
class Greeter
	# Define the method hello
	# This method greets the
	# user
	def hello(name)
		puts &quot;Hello &quot; + name
	end # End of the method hello
end # End of the class Greeter

Greeter.new.hello(&quot;Tyler&quot;)
</pre>
<p><strong><br />
</strong></p>
<p><strong>Transcript:</strong></p>
<p>Hello everybody and welcome to Programming With Ruby Episode 4, Main<br />
Ruby Concepts.</p>
<p>Covered in this episode. We&#8217;ll be playing with interactive ruby (also</p>
<p>called irb). I will teach you about variables, basic methods, and</p>
<p>classes in Ruby. Lets get started!</p>
<p><strong>Basics Description</strong></p>
<p>Ruby is an object oriented programming language. object oriented<br />
languages use objects. Ruby goes beyond most other object oriented<br />
languages, because in Ruby, everything is an object. (I&#8217;ll show you<br />
exactly what that means in a few minutes)</p>
<p>Object oriented programming sort of models real life. Look around you,<br />
everything around you is an object. Your computer, your desk, books,<br />
the moon, and people are all objects.</p>
<p>In programming, all objects have properties called variables. These<br />
could be the color of the object, the weight, size, or any other kind<br />
of property.</p>
<p>Objects also have methods (which are sometimes called functions). A<br />
camera object would have a method to take pictures. A car would have a<br />
method to drive. A printer would have a method to print.</p>
<p>As well as making code easier to understand, you will also continue to<br />
appreciate other benefits of object oriented programming further on in<br />
your programming career.</p>
<p><strong>Example</strong></p>
<p>I have written a very basic Ruby program. This is about what most<br />
programs look like, just a lot simpler. Let&#8217;s break it down line by<br />
line.</p>
<p>Everything followed by a hash mark (#), is ignored by Ruby. These are<br />
called comments. In these comments you can describe what your code is<br />
doing, make notes to yourself, and other such things.</p>
<p>The first line says &#8220;class Greeter&#8221;. This line tells Ruby that we are<br />
now defining a class named &#8220;Greeter&#8221;. Please remember that all classes<br />
in Ruby start with a capital letter!</p>
<p>The next line says &#8220;def hello(name)&#8221;. This line means we are defining<br />
a function (def), named &#8220;hello&#8221;, that takes the parameter &#8220;name&#8221;.</p>
<p>The following line is a little trickier. &#8216;puts &#8220;Hello &#8221; +<br />
name&#8217;. &#8220;puts&#8221; means &#8220;put string&#8221;. our string is &#8220;Hello &#8221; + name. Now<br />
you&#8217;re probably wondering what the &#8220;+ name&#8221; is for. We&#8217;re aren&#8217;t doing<br />
math on strings, but we are connecting the variable &#8220;name&#8221; to our other<br />
string &#8220;Hello &#8220;.</p>
<p>The next two lines have the keyword &#8220;end&#8221;. The first end means we are<br />
done defining the function &#8220;hello&#8221;. The second &#8220;end&#8221; means we are done<br />
defining the class &#8220;Greeter&#8221;.</p>
<p>Below where the class &#8220;Greeter&#8221; ends we have the line &#8216;myname =<br />
&#8220;Tyler&#8221;&#8216;. This means are creating the variable myname and placing<br />
&#8220;Tyler&#8221; inside of it.</p>
<p>Next is, the line &#8220;person = Greeter.new&#8221;. This means we are<br />
instantiating a new greeter object named person.</p>
<p>Finally we have the line &#8220;person.hello(myname)&#8221;. This calls the<br />
person object&#8217;s method &#8220;hello&#8221; and passes the variable &#8220;myname&#8221; as an<br />
argument.</p>
<p>Now lets run the program. If you remember, to run a program you open<br />
Terminal or Command Prompt, change directories into where you saved<br />
your program and type &#8220;ruby programname.rb&#8221;. In my case I will type<br />
&#8220;ruby episode4.rb&#8221;.<br />
And there you have it, it says &#8220;Hello Tyler&#8221;</p>
<p>The code in this example will be available in the YouTube video<br />
description, and below this video on manwithcode.com</p>
<p><strong>Example 2</strong><br />
Back to the code. This code is actually a little longer<br />
than it has to be. I did this so things would hopefully make more<br />
sense. In reality we can change the last three lines to<br />
&#8216;Greeter.new.hello(&#8220;Tyler&#8221;)&#8217;</p>
<p>We can do this because in Ruby, everything is an expression. Here&#8217;s how<br />
it breaks down. &#8220;Greeter.new&#8221; creates a greeter object. &#8220;.hello&#8221; calls<br />
the method &#8220;hello&#8221; on that new object. And then we pass in the string<br />
&#8220;Tyler&#8221; as an parameter.</p>
<p>Ruby has many other tricks like this to make your code shorter. Some<br />
have disadvantages and others do not. One of the most important<br />
disadvantages can be clarity or readability. You want your code to be<br />
as easy to understand as possible.</p>
<p><strong>irb</strong><br />
Interactive Ruby (or irb for short). irb makes it very easy to<br />
quickly test out code, find out if/how something works, do basic math,<br />
and write throwaway code you will only use once.</p>
<p>To open irb, open up your Command Prompt or Terminal and enter<br />
irb. Alternatively, if you are on Windows, go into All Programs and<br />
under Ruby will be a program name fxri which is an equivalent to irb.</p>
<p>In irb you can do basic math:</p>
<pre class="brush: ruby; title: ; notranslate">
2 + 2 #=&gt; 4
2**6 #=&gt; 64
2 * 5 #=&gt; 10
</pre>
<p>We can write any valid Ruby code. So we could write the greet method again.</p>
<pre class="brush: ruby; title: ; notranslate">
def greet(name)
    puts &quot;Hello &quot; + name
end
</pre>
<p>And then call it:</p>
<pre class="brush: ruby; title: ; notranslate">
greet(&quot;Tyler&quot;)
</pre>
<p>Remember earlier I said everything in Ruby was an object? I&#8217;ll show<br />
you what I mean. if you type in the name of an object and call the<br />
class method it will tell you its class.</p>
<pre class="brush: ruby; title: ; notranslate">
0.class #=&gt; Fixnum
&quot;Hello&quot;.class #=&gt;; String
String.class #=&gt; Class
</pre>
<p>I encourage you to play around with irb for a little while, try<br />
writing your own methods, and have some fun with it.</p>
<p><strong>End</strong><br />
This brings us to the end of this episode, I hope it helped<br />
you.  If you need any help, have questions or comments, leave a<br />
comment below or contact me at tyler@manwithcode.com</p>
<p>Don&#8217;t forget to donate! There is a donation link to the right of this<br />
video. On YouTube its in the description box. On my website, it is to<br />
the right of the video.</p>
<p>Thanks for watching! Bye!</p>
]]></content:encoded>
			<wfw:commentRss>http://manwithcode.com/84/programming-with-ruby-episode-4-main-ruby-concepts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<series:name><![CDATA[Ruby Programming]]></series:name>
	</item>
	</channel>
</rss>

