<?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; open classes</title>
	<atom:link href="http://manwithcode.com/tag/open-classes/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 10, Objects and Modules</title>
		<link>http://manwithcode.com/169/programming-with-ruby-episode-10-objects-and-modules/</link>
		<comments>http://manwithcode.com/169/programming-with-ruby-episode-10-objects-and-modules/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 05:55:13 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Ruby Programming]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[open classes]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scope]]></category>

		<guid isPermaLink="false">http://manwithcode.com/?p=169</guid>
		<description><![CDATA[Part 1: Part 2: Covered In This Episode Variable Scope Class creation Open Classes Class Inheritance Modules Transcript: Hello Everybody and welcome to Programming With Ruby Episode 10, Objects and Modules. As always, I&#8217;m Tyler and this video is brought to you by manwithcode.com Variable scope will be explained. In this episode I will be [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Part 1:</strong><br />
<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/q75BiSgI6QI&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/q75BiSgI6QI&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />
<strong>Part 2:</strong><br />
<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/AmOj09AVI8k&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/AmOj09AVI8k&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />
<strong>Covered In This Episode</strong></p>
<ul>
<li>Variable Scope</li>
<li>Class creation</li>
<li>Open Classes</li>
<li>Class Inheritance</li>
<li>Modules</li>
</ul>
<p><strong>Transcript:</strong></p>
<p>Hello Everybody and welcome to Programming With Ruby Episode 10,<br />
Objects and Modules. As always, I&#8217;m Tyler and this video is brought to<br />
you by manwithcode.com</p>
<p>Variable scope will be explained.</p>
<p>In this episode I will be going over class creation, I touched on this<br />
before but that was a while ago and I will also be going more in-depth</p>
<p>I will be teaching you what class inheritance is.</p>
<p>You will also find out what open classes are, and why they are useful.</p>
<p>You will learn what modules are, and how and when you should use them</p>
<p>Lets get started!</p>
<p><strong>Variable Scope</strong></p>
<p>I taught you about variables earlier, but I need to go a little more<br />
in-depth for you to be able to write real applications, and not be<br />
confused by some mysterious errors.</p>
<p>What is a variable scope? a variable scope is where the variable is<br />
available for use in the program. The code in classes and methods that<br />
you define have a different scope than the code outside<br />
them. Different scopes are introduced when classes and methods are<br />
defined.</p>
<p>There are 5 different types of variables:<br />
1. local variable ex: variable<br />
2. instance variable ex: @variable<br />
3. class variable ex: @@variable<br />
4. global variable ex: $variable<br />
5. constant variables ex: VARIABLE</p>
<p>A local variable is available in the scope in which it is defined. An<br />
instance variable is available in the instance of the class it was<br />
defined. A class variable is available from any instances of that<br />
class. A global variable is available anywhere. A constant is<br />
available anywhere, but can only be changed within the scope it was<br />
defined.</p>
<p><strong>Class Creation</strong></p>
<p>As mentioned in episode 4 you define classes like this, don&#8217;t forget<br />
that classes must start with an upper case letter:</p>
<pre class="brush: ruby; title: ; notranslate">
class MyClass
end
</pre>
<p>and create them like this:</p>
<pre class="brush: ruby; title: ; notranslate">
variable = MyClass.new
</pre>
<p>You can define methods inside the class:</p>
<pre class="brush: ruby; title: ; notranslate">
class MyClass
    def hello
        puts 'Hello!'
    end
end
</pre>
<p>If you define a method named &#8216;initialize&#8217;, that method is run when the<br />
class is instantiated. This is very useful in many situations, like<br />
creating a screen in a game or connecting to the database in a web<br />
application. This is also the usual place for defining instance variables</p>
<pre class="brush: ruby; title: ; notranslate">
class MyClass
    def initialize
        @database = connect_to_database
    end
end
</pre>
<p>A method defined with &#8216;self.&#8217; in front of it&#8217;s name is called a class<br />
method, because the method is available outside of the class, and you<br />
don&#8217;t have to instantiate an object. These can be useful for times<br />
when you don&#8217;t want create objects, or want certain information about<br />
all the instances of a class.</p>
<pre class="brush: ruby; title: ; notranslate">
class MyClass
    def self.game_objects
        # Return all objects in the game
    end
end
</pre>
<p><strong>Open Classes</strong></p>
<p>So now that you know more about creating classes, I would like to call<br />
your attention to a very useful feature of Ruby, Open Classes.</p>
<p>The term Open classes means you have the ability to add or substitute<br />
code in a class that is already defined. This is quite easy to do too,<br />
all you have to do is define a class in the same way you always do,<br />
just with a pre-existing class. You only have to define what you are<br />
adding, or overwriting, you don&#8217;t have to define the WHOLE class<br />
again.</p>
<p>Take for example, the String class. This class (obviously) is the<br />
class from which all strings are created. Lets say, for example that<br />
you had some code that took a string, and gave back an array that had<br />
each word in it. You could do this:</p>
<pre class="brush: ruby; title: ; notranslate">
def words(string)
    string.scan(/\w[\w\'\-]*/)
end
words(&quot;Hello World&quot;) #=&gt; [&quot;Hello&quot;, &quot;World&quot;]
</pre>
<p>But it looks a lot nicer, and is more object-oriented if you do this instead:</p>
<pre class="brush: ruby; title: ; notranslate">
class String
    def words
        scan(/\w[\w\'\-]*/)
    end
end
&quot;Hello World&quot;.words #=&gt; [&quot;Hello&quot;, &quot;World&quot;]
</pre>
<p>You can probably see why this can be useful.</p>
<p>Be careful though, if you override existing functionality, you run the<br />
risk of breaking that functionality in your code, and all the external<br />
code your project uses.</p>
<p><strong>Class Inheritance</strong></p>
<p>Lets say you are making a video game. In that game you will have many<br />
different types of enemies.</p>
<p>Now, odds are that all your enemies will have stuff in common. They<br />
probably will all have health, ammo, etc. They all will have to draw<br />
themselves on the screen, animate when they move, etc.</p>
<p>So you can see that with many different types of enemies, you would<br />
have to have lots of duplicate code in each class. This is solved via<br />
class inheritance. Class inheritance allows you to write one class<br />
that contains all the common code. Then when you create other classes<br />
you can specify that those classes will use (inherit) that common<br />
code.</p>
<p>You specify if a class is inheriting from another by following the<br />
class name, with a less than sign followed by the name of the class<br />
you are inheriting from.</p>
<p>You can also re-implement some of that common code, if needed.</p>
<p>For example:</p>
<pre class="brush: ruby; title: ; notranslate">
class Enemy
    def draw
        # Drawing Code
    end
end

class Soldier &lt; Enemy
    def move
    end

    def shoot
    end
end

class DifferentEnemy &lt; Enemy
    def draw
        # Changes the draw functionality, only for this class
    end
end
</pre>
<p>Another thing to keep in mind, if you redefine the initialize method<br />
in your inherited class, you must call the super method.</p>
<p><strong>Modules</strong></p>
<p>Modules are like classes, except they can&#8217;t be initialized, and every<br />
method has to be prefixed with &#8220;self.&#8221; like class methods.</p>
<p>This limitation in functionality may make you wonder when modules are<br />
ever useful. There are actually only a few times. First is to keep<br />
parts of your code separated. The more important second reason is when<br />
you are creating a library of code for others to use. This is so the<br />
functionality in the library isn&#8217;t stepping over or redefining classes<br />
you have already defined (if they happen to have the same name).</p>
<p>You define modules just like you do classes, except using the module keyword:</p>
<pre class="brush: ruby; title: ; notranslate">
module DataVisualizer
    class Grapher
    end

    class Plotter
    end

    def self.data_compatible?
    end
end
</pre>
<p>To access methods that modules define you simply do:</p>
<pre class="brush: ruby; title: ; notranslate">
module MyModule
    def self.x
    end
end
MyModule.x
</pre>
<p>To access classes defined by modules, you have to use double colons:</p>
<pre class="brush: ruby; title: ; notranslate">
module DataVisualizer
    class Grapher
    end

    class Plotter
    end
end

DataVisualizer::Grapher.new
</pre>
<p>That&#8217;s all there is to know about modules, meaning this is the end of<br />
the episode!</p>
<p>Please donate, those these videos are free, it costs money to make them.</p>
<p>If you have any questions comments or suggestions about anything<br />
related to Man With Code or the Ruby tutorials, you can leave a<br />
comment on this page or email me at tyler@manwithcode.com</p>
<p>Than you very much for watching, goodbye!</p>
]]></content:encoded>
			<wfw:commentRss>http://manwithcode.com/169/programming-with-ruby-episode-10-objects-and-modules/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<series:name><![CDATA[Ruby Programming]]></series:name>
	</item>
	</channel>
</rss>

