{"id":105,"date":"2009-08-16T21:46:46","date_gmt":"2009-08-17T01:46:46","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=105"},"modified":"2024-03-01T17:49:59","modified_gmt":"2024-03-01T22:49:59","slug":"ruby-koans","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2009\/08\/16\/ruby-koans\/","title":{"rendered":"Ruby Koans"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/Aug2009\/koans.jpg\" alt=\"Koans\" title=\"Koans\" style=\"float:left; padding: .5em\" \/>I decided to tackle a little Ruby language learning this weekend.  So yesterday, I downloaded the <a href=\"http:\/\/github.com\/edgecase\/ruby_koans\/tree\/master\" alt=\"Ruby Koans\" title=\"Ruby Koans\">Ruby Koans<\/a> and went to work.  The Koans were created by EdgeCase to help people learn Ruby.  Basically, they are a set of unit tests that exercise aspects of Ruby.  To begin, all of the tests fail.  You have to write code or jump back and forth to the Interactive Ruby Shell (IRB) and get the results of the statements to make the asserts pass.<\/p>\n<p>I went through and did 105 unit tests yesterday, each of them having several parts to them.  Having messed around a little with Rails, I was familiar with using IRB and Rake and a little of the syntax.  In my opinion, the way this is set up and builds upon itself is really helpful and is a great way to teach Ruby to n00bs.  I really salute Jim Weirich and Joe O&#8217;Brien for their work in getting these together.<\/p>\n<p>One of the exercises is to create a triangle method that takes three parameters (the sizes of the sides).  You then have to decide what kind of triangle it is.  After those tests pass, your next assignment is to prevent 0 length or negative sides as well as making sure your triangle validates.  <\/p>\n<p>I wrote all the code and passed all the tests except for the triangle validation test.  I was having a serious brain cramp and could not remember how you ensured that you&#8217;ve gotten the correct sides for any triangle (and not just a right triangle), so I looked for someone else&#8217;s example of this code.  I found this code:<\/p>\n<pre>\r\n# Triangle Project Code.\r\n\r\n# Triangle analyzes the lengths of the sides of a triangle\r\n# (represented by a, b and c) and returns the type of triangle.\r\n#\r\n# It returns:\r\n#   :equilateral  if all sides are equal\r\n#   :isosceles    if exactly 2 sides are equal\r\n#   :scalene      if no sides are equal\r\n#\r\n# The tests for this method can be found in\r\n#   about_triangle_project.rb\r\n# and\r\n#   about_triangle_project_2.rb\r\n#\r\ndef triangle(a, b, c)\r\n  if a&lt;=0 or b&lt;=0 or c&lt;=0 \r\n    fail TriangleError, \"Sides must all have length greater than 0.\"\r\n  end\r\n\r\n  myArray = [a,b,c]\r\n  maxSide = myArray.max\r\n  total = 0\r\n  myArray.each { |myEle| total += myEle }\r\n  if total - maxSide &lt;= maxSide\r\n    fail TriangleError, \"Sum of shortest two sides must be greater than length of longest side.\"\r\n  end\r\n\r\n  if a==b and a==c\r\n     :equilateral \r\n  elsif a!=b and b!=c and a!=c\r\n    :scalene\r\n  else\r\n     :isosceles\r\n  end\r\nend\r\n\r\n# Error class used in part 2.  No need to change this code.\r\nclass TriangleError &lt; StandardError\r\nend\r\n<\/pre>\n<p>Okay, I see.  The smallest two sides added together need to be greater than the third side.  However, I thought that the author of this code greatly over-complicated this process.  We are always dealing with a fixed number of sides and the trick is to put them in order and then add away.  I figured Ruby had a simple array sort (and they do), so I would place the sides into an array, sort it, and then add the 0 index and 1 index spot and make sure that it was greater than the 2 index spot.  Case Closed and (in my opinion) a bit easier to read.  Forgiving of course, that I didn&#8217;t pass messages in my exceptions, I personally like my code better (of course \ud83d\ude09 ).  I also appreciate that Ruby doesn&#8217;t mind me putting ( and ) around my if conditions.  I just feel safer for them to be contained \ud83d\ude09 <\/p>\n<pre>\r\n# Triangle Project Code.\r\n\r\n# Triangle analyzes the lengths of the sides of a triangle\r\n# (represented by a, b and c) and returns the type of triangle.\r\n#\r\n# It returns:\r\n#   :equilateral  if all sides are equal\r\n#   :isosceles    if exactly 2 sides are equal\r\n#   :scalene      if no sides are equal\r\n#\r\n# The tests for this method can be found in\r\n#   about_triangle_project.rb\r\n# and\r\n#   about_triangle_project_2.rb\r\n#\r\ndef triangle(a, b, c)\r\n  # WRITE THIS CODE\r\n  if (a &lt;= 0 or b &lt;= 0 or c&lt;=0)\r\n    raise TriangleError\r\n  end\r\n  \r\n  sides = [a, b, c].sort\r\n  \r\n  if (sides[0] + sides[1] &lt;= sides[2])\r\n    raise TriangleError\r\n  end\r\n  \r\n  if (a==b and a==c)\r\n    value = :equilateral\r\n  else\r\n    if (a==b or a==c or b==c)\r\n      value = :isosceles\r\n    else\r\n      value = :scalene\r\n    end    \r\n  end  \r\nend\r\n\r\n# Error class used in part 2.  No need to change this code.\r\nclass TriangleError &lt; StandardError\r\nend\r\n<\/pre>\n<p>As I do these Koans, I&#8217;ve been making note of things I&#8217;d like to include in future blog entries about things I&#8217;ve enjoyed about the language and will be posting them in the near term.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I decided to tackle a little Ruby language learning this weekend. So yesterday, I downloaded the Ruby Koans and went to work. The Koans were created by EdgeCase to help people learn Ruby. Basically, they &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[45,105],"class_list":["post-105","post","type-post","status-publish","format-standard","hentry","category-ruby","tag-koans","tag-ruby"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=105"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/105\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}