{"data":{"wordpressPost":{"id":"88a7f167-e32e-5975-8ae4-c0e3ec7dd00c","title":"A Ruby Newb&#8217;s Intro to Classes and Object-Orientedness","slug":"ruby-newbs-intro-classes-object-orientedness","content":"<p>Dearth of beginner-friendly info on the web got you down? Don&#8217;t worry, I&#8217;m going to explain Ruby&#8217;s classes and this weird object-oriented thing for you.</p>\n<h3>The Beginner&#8217;s Path</h3>\n<p>So you&#8217;re just starting out learning a programming language, and you are introduced to everyone&#8217;s favorite starter script, Hello World.</p>\n<blockquote>\n<pre>puts \"Hello World\"</pre>\n</blockquote>\n<p>You quickly move on to writing little scripts that reinvent the mathematical wheel, because maths are bits of logic that are easy to reproduce for yourself, and, frankly, because you&#8217;re not capable of much more. (Don&#8217;t worry! Everyone starts out this way.)</p>\n<blockquote>\n<pre>def multiply(x, y)\r\n  x * y\r\nend</pre>\n</blockquote>\n<p>Then you&#8217;re writing stuff that&#8217;s actually kinda useful, like this script that turns a number into a string and inserts commas in the appropriate places:</p>\n<blockquote>\n<pre>def separate_comma(number)\r\n  if number &lt; 1000     \r\n    number.to_s   \r\n  elsif number &gt; 999 &amp;&amp; number &lt; 1000000\r\n    number.to_s.insert(-4, \",\")\r\n  else\r\n    number.to_s.insert(-4, \",\").insert(-8, \",\")\r\n  end\r\nend</pre>\n</blockquote>\n<p>YOU ARE ROCKSTAR NOOB CODER. But&#8230; there&#8217;s something nobody told you.</p>\n<p><strong>This isn&#8217;t really what Ruby programming looks like.</strong></p>\n<h2>Object-Oriented Languages</h2>\n<p>What you&#8217;ve been writing is in a &#8220;procedural&#8221; format, where you have some data (the numbers 1 and 2) and then you do things to it ( + ). You think that Ruby has special keywords and operators that you can use to do things to your Fixnum data.</p>\n<p>Well, I can&#8217;t tell you that this is 100% wrong. There is proceduralness in Ruby. But what&#8217;s <em>really</em> going on?</p>\n<p>See that fixnum of yours? It isn&#8217;t data. It is an <strong>object</strong>. And that means: your Fixnum object not only contains the <em>data</em>, 2, but it also <em>knows what methods will work on it</em>.</p>\n<p>Did you catch that? If you have an <em>instance</em> of Fixnum, an occurance of a number, it knows not only the number but also <strong>+</strong>, <strong>&#8211;</strong>, <strong>*</strong>, and every other action that can be performed on a Fixnum.</p>\n<p>(My response to this when I first learned it: Wow, OOP is magic!)</p>\n<h3>Classes</h3>\n<p>You see what&#8217;s going on is that instead of <em>data types</em> (string, integer, array, etc), Ruby has <em>classes</em>.</p>\n<p>When Yukihiro Matsumoto wrote Ruby, he defined the classes Fixnum, String, etc., and made it so that every time you create a new string, that specific string has a built-in knowledge of concatenation, splitting, etc.</p>\n<p>Well, you might say, so what? <strong>puts &#8220;Hello World&#8221;</strong> still looks and works the same whether or not you understand what&#8217;s going on under the hood. Why is this important to know? And what the heck are the implications of this crazy object-oriented thing?? (&#8220;Why?&#8221; and &#8220;What are the implications?&#8221; are the two least-answered questions in beginner&#8217;s tutorials, and yet sometimes the most important to answer, IMHO.)</p>\n<p>If you have classes that define objects and the actions associated with them, well then guess what? You can create your own classes too! Yes, we&#8217;re no longer limited to Fixnums, Strings, Hashes, etc. In fact, we can create:</p>\n<h4>The Puppy Class</h4>\n<p>A lot of examples of how to write and use classes involve a Person class, or a Car class or something similar. For MY example, I&#8217;m going to use puppies. Because&#8230; puppies.</p>\n<blockquote>\n<pre>class Puppy\r\nend</pre>\n</blockquote>\n<p>Well, I guess we&#8217;ve established that there&#8217;s a Puppy class, but it&#8217;s not very interesting or useable. Let&#8217;s define what an instance of this Puppy class looks like, and then create a new puppy.</p>\n<blockquote>\n<pre>class Puppy\r\n  def initialize(age) <em># age in months</em>\r\n    @age = age\r\n  end\r\nend\r\n\r\nPuppy.new(2)</pre>\n</blockquote>\n<p>A two month old puppy! Congratulations, you must be very proud.</p>\n<p>So what is this <strong>initialize</strong> stuff? That is where you specify what your new object will be like when you <strong>.new</strong> it to create a new one. This particular class takes a single argument, <strong>age</strong> (in months). Then it creates an instance variable <strong>@age</strong>, to make its age available throughout the rest of the class.</p>\n<p>Wait &#8211; have you learned about scope yet? If not: there&#8217;s a rule that says you can only access variables within their own &#8220;scope.&#8221;&#8221; I think the simplest way to explain scope is that if a variable is defined in a <strong>def&#8230;end</strong> block or a <strong>do&#8230;end</strong> block or <strong>{&#8230;}</strong>, you can only use it inside that block and it&#8217;s NOT accessible outside that block.</p>\n<p>An <em>instance variable</em>, that version with <strong>@</strong> in front, can be used anywhere inside that instance of the class. So:</p>\n<blockquote>\n<pre>class Puppy\r\n  def initialize(age)\r\n    @age = age\r\n  end\r\n\r\n  def dire_warning <em> # we don't have to pass an argument here because </em>\r\n    if @age &lt;= 6    <em># we're using an instance variable!</em>\r\n      puts \"Goodbye, free time!\"\r\n    elsif @age &lt; 12\r\n      puts \"Goodbye, new shoes!\"\r\n    else\r\n      puts \"Hello, best friend!\"\r\n    end\r\n  end\r\nend</pre>\n</blockquote>\n<p>and</p>\n<blockquote>\n<pre>Puppy.new(2).dire_warning <em>#=&gt; Goodbye, free time!</em></pre>\n</blockquote>\n<p>And now we can put all sorts of puppy-related methods inside of this class, for when we need our instance of puppy to have some kind of action going on with it.</p>\n<h2>So Did You Get All That?</h2>\n<h4>In review:</h4>\n<ul>\n<li>Ruby has objects, not data types</li>\n<li>Objects know their data and the methods associated with them</li>\n<li>Instance variables are great for allowing different methods within a class to get to variables that were assigned elsewhere within that class</li>\n<li>Two month old puppies are a lot of work. Make sure you buy that special pet stain remover. Really.</li>\n</ul>\n","date":"July 06, 2014","categories":[{"name":"ones and zeros","slug":"ones-and-zeros"}],"tags":null,"author":{"name":"jessica","slug":"jessica"}}},"pageContext":{"id":"88a7f167-e32e-5975-8ae4-c0e3ec7dd00c"}}