<?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/"
	>

<channel>
	<title>Shlrm.org Blag &#187; Ruby</title>
	<atom:link href="http://shlrm.org/wordpress/category/coding/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://shlrm.org/wordpress</link>
	<description>Linux, Java, Ruby, and Politics.</description>
	<lastBuildDate>Tue, 27 Jul 2010 04:08:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Building ruby 1.9 on Fedora 13</title>
		<link>http://shlrm.org/wordpress/2010/06/19/building-ruby-1-9-on-fedora-13/</link>
		<comments>http://shlrm.org/wordpress/2010/06/19/building-ruby-1-9-on-fedora-13/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 18:19:23 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[fedora]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=378</guid>
		<description><![CDATA[Was harder than it should&#8217;ve been. I got annoyed. You need to install openssl-devel, zlib-devel, bison, gcc, make, patch, tar, and maybe gcc-c++ (although I don&#8217;t think this one is needed). Go get the latest ruby 1.9 source, as of this writing 1.9-p378, and extract it somewhere. Then go get the patches on this bug, [...]]]></description>
			<content:encoded><![CDATA[<p>Was harder than it should&#8217;ve been. I got annoyed.</p>
<p>You need to install openssl-devel, zlib-devel, bison, gcc, make, patch, tar, and maybe gcc-c++ (although I don&#8217;t think this one is needed).</p>
<p>Go get the <a href="ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.1-p378.tar.gz">latest ruby 1.9 source</a>, as of this writing 1.9-p378, and extract it somewhere. Then go get the patches on <a href="http://redmine.ruby-lang.org/issues/show/2022#note-11">this bug, at the specific comment</a>. You will need to apply at least the openssl-build-fix patch, since fedora uses openssl 1.0 and it&#8217;s not yet into ruby 1.9. Then follow your typical ./cofnigure, make, and make install stuff. I installed mine into a prefix of /opt/ruby so that it wouldn&#8217;t affect any fedora ruby stuff that it might want. I then added ruby&#8217;s path to the end of my user&#8217;s PATH variable.</p>
<p>That&#8217;ll get you a working ruby 1.9 in Fedora.</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2010/06/19/building-ruby-1-9-on-fedora-13/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>vim syntax hilighting for ruby/rails</title>
		<link>http://shlrm.org/wordpress/2010/05/18/vim-syntax-hilighting-for-rubyrails/</link>
		<comments>http://shlrm.org/wordpress/2010/05/18/vim-syntax-hilighting-for-rubyrails/#comments</comments>
		<pubDate>Wed, 19 May 2010 02:10:13 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[W00t!]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=369</guid>
		<description><![CDATA[http://www.vim.org/scripts/script.php?script_id=1567 Woot. EDIT: Man, this guy has all sorts of good stuff: http://www.vim.org/account/profile.php?user_id=9012 cucumber, rails, ruby, git-vim integration. VIM FTW!]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.vim.org/scripts/script.php?script_id=1567">http://www.vim.org/scripts/script.php?script_id=1567</a></p>
<p>Woot.</p>
<p>EDIT:</p>
<p>Man, this guy has all sorts of good stuff: <a href="http://www.vim.org/account/profile.php?user_id=9012">http://www.vim.org/account/profile.php?user_id=9012</a></p>
<p>cucumber, rails, ruby, git-vim integration. VIM FTW!</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2010/05/18/vim-syntax-hilighting-for-rubyrails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Groovy!</title>
		<link>http://shlrm.org/wordpress/2009/01/12/groovy/</link>
		<comments>http://shlrm.org/wordpress/2009/01/12/groovy/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 17:09:36 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=175</guid>
		<description><![CDATA[Reading about groovy. It&#8217;s got some really interesting things. It brings the magic of dynamic typing and such (like ruby and python have) to java. But it doesn&#8217;t replace java, it simply adds it to java. So when you&#8217;re writing a groovy script, you can actually just write pure java and it&#8217;ll still work. That&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Reading about groovy. It&#8217;s got some really interesting things. It brings the magic of dynamic typing and such (like ruby and python have) to java. But it doesn&#8217;t replace java, it simply adds it to java. So when you&#8217;re writing a groovy script, you can actually just write pure java and it&#8217;ll still work. That&#8217;s a very interesting (and potentially evil) feature. Imagine code with groovy and java intermingled. Yikes.</p>
<p>Anyway, there&#8217;s also something called grails. It&#8217;s groovy&#8217;s rails application framework. I&#8217;m hoping that it&#8217;ll be more useful to me than ruby on rails was. Whilst I was able to get some stuff done in rails, I wasn&#8217;t able to do quite what I wanted to do. Things didn&#8217;t interoperate the way I wanted them to. Building things to be completely REST-ful was a pain. It may simply be that I don&#8217;t know enough to do fully RESTful applications.</p>
<p>Also, testing was a pain in rails. I found several testing frameworks, but none quite fit what I wanted to do. That may have had something to do with my insistence upon using constraints in my database. Rails isn&#8217;t built to handle constraints, it wants your database to be dumb. I don&#8217;t really think that&#8217;s a good thing; having constraints in your database allows the database to make more intelligent optimizations regarding the data. Since rails assumes there aren&#8217;t any constraints on the database, it generates tests that break databases that use constraints. There are a few work-arounds, but none that exist well enough to actually run all my tests at once. I can piecemeal them and if I do them in the correct order, everything works just fine.</p>
<p>Regarding ruby itself, I miss the ability to create threads. I&#8217;ve finally understood threading in java, and how to efficiently and effectively use threads to handle things. Lacking a similar threading operation in ruby (at least an obvious one) I had a lot of trouble trying to implement some of the things I wanted to do. I love the dynamic-ness of ruby, and I like the ability to just write code and it does mostly what I think it should. There is probably a good way to do threading in ruby, I mean, people have written webservers entirely in ruby, I just haven&#8217;t figured it out. I&#8217;ll probably still use ruby for things, I&#8217;ve got a project or two churning around in the back of my mind to use ruby on, but for now, I think I&#8217;ll stick with Java.</p>
<p>This has turned more into a rant about what I don&#8217;t like about ruby and rails. I guess I&#8217;m hoping that grails will live up to my expectations more than rails did, and that groovy will give me the dynamic fun that I enjoyed with ruby.</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2009/01/12/groovy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My First Ruby Proggie</title>
		<link>http://shlrm.org/wordpress/2008/07/29/my-first-ruby-proggie/</link>
		<comments>http://shlrm.org/wordpress/2008/07/29/my-first-ruby-proggie/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 18:43:04 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=116</guid>
		<description><![CDATA[I wrote this to provide me a simple wget type program. require 'net/http' require 'uri' proxy_addr = 'localhost' proxy_port = 8118 url = URI.parse('http://shlrm.org/baconflowchartvj8.jpg') Net::HTTP::Proxy(proxy_addr, proxy_port).start(url.host) do &#124;request&#124; puts "Connected to teh proxy" # connected to mah proxy to_save = url.path.split(/\//)[-1] File.open("c:\\" + to_save, 'wb') do &#124;f&#124; puts "opened file" request.request_get(url.path) do &#124;response&#124; content_length = [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote this to provide me a simple wget type program.</p>
<pre class="brush:ruby">
require 'net/http'
require 'uri'

proxy_addr = 'localhost'
proxy_port = 8118

url = URI.parse('http://shlrm.org/baconflowchartvj8.jpg')

Net::HTTP::Proxy(proxy_addr, proxy_port).start(url.host)   do |request|
  puts "Connected to teh proxy"
  # connected to mah proxy
  to_save = url.path.split(/\//)[-1]
  File.open("c:\\" + to_save, 'wb') do |f|
    puts "opened file"
    request.request_get(url.path) do |response|
      content_length = Integer(response['content-length'])
      total_count = 0
      response.read_body do |str|
        if str.length &gt; 0 then
          total_count += str.length
          print "Complete: #{total_count} of #{content_length}:  "
          puts "%4f" % (1.0*total_count/content_length*100)
        end
        f.write str
      end
    end
    puts "Written file"
  end
  puts "done connecting"
end
puts "all done"</pre>
<p>So that&#8217;s it.</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2008/07/29/my-first-ruby-proggie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby: Unit Tests and Debugging</title>
		<link>http://shlrm.org/wordpress/2008/07/29/ruby-unit-tests-and-debugging/</link>
		<comments>http://shlrm.org/wordpress/2008/07/29/ruby-unit-tests-and-debugging/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 14:49:03 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=114</guid>
		<description><![CDATA[Well, this is all that&#8217;s left for learning the core of the language. How to use ruby&#8217;s built in Unit Testing framework and how to fiddle with the debugger. This post will be very short, because there&#8217;s not a whole lot to unit testing. Going over the debugger is kinda silly too, since it&#8217;s mostly [...]]]></description>
			<content:encoded><![CDATA[<p>Well, this is all that&#8217;s left for learning the core of the language. How to use ruby&#8217;s built in Unit Testing framework and how to fiddle with the debugger. This post will be very short, because there&#8217;s not a whole lot to unit testing. Going over the debugger is kinda silly too, since it&#8217;s mostly just a command reference. Also, I found an interesting gotcha in the examples listed that I&#8217;ll share with you. There&#8217;s a benchmarking and profiling module worth saying a few words about.<span id="more-114"></span></p>
<p>Unit testing in ruby is just like unit testing in any other language. Assert_* methods abound.</p>
<p>Using the debugger is similar to using gdb or some other debugger as well. There&#8217;s a list of commands, you can set breakpoints and watch variables. Nothing very surprising there.</p>
<p>The author offers up a &#8220;checklist&#8221; for going through your program to ensure that it all works. He firstly reccomends that you run the program with -w. Keep warnings on, so you can resolve issues. He mentions an interesting issue with precendence regarding {} and do&#8230;end. To quote the example source code from the book:</p>
<pre lang="ruby">def one(arg)
  if block_given?
    "block given to 'one' returns #{yield}"
  else
    arg
  end
end
def two
  if block_given?
    "block given to 'two' returns #{yield}"
  end
end
result1 = one two {
  "three"
}
result2 = one two do
  "three"
end
puts "With braces, result = #{result1}"
puts "With do/end, result = #{result2}"</pre>
<p>Which produces:</p>
<pre>With braces, result = block given to 'two' returns three
With do/end, result = block given to 'one' returns three</pre>
<p>So that&#8217;s something to keep in mind. Another gotcha that caught me, is that output may be buffered. I was writing a program to theif images from a website, and I wanted to use print &#8220;.&#8221; to show completion of an image, but the software wouldn&#8217;t show anything. puts &#8220;.&#8221; would work, but that put a newline character. I hacked around it by calling $stdout.flush to force it to put the . to the console. Setting &#8220;sync&#8221; on the thing may have worked better.</p>
<p>Also of note is a benchmark module built into ruby. You can profile and run benchmarks and it&#8217;ll provide you pretty statistics after running. I&#8217;ve used the java profiler before in the past, and it can really help you identify slow parts of your software.</p>
<p>That&#8217;s mostly the core of the language, and how to build programs. Now, I get into more of the grunt work of building ruby software. I&#8217;ll have to look into section two and see if I&#8217;m going to jump directly into rails or not&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2008/07/29/ruby-unit-tests-and-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby: Fibrous Content Ahead</title>
		<link>http://shlrm.org/wordpress/2008/07/28/ruby-fibrous-content-ahead/</link>
		<comments>http://shlrm.org/wordpress/2008/07/28/ruby-fibrous-content-ahead/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 23:35:18 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[fibers]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=111</guid>
		<description><![CDATA[Fibers, threads, and processes! Oh, my! Fibers are probably the most interesting part of this, and ruby&#8217;s slightly broken threading model. Processes are interesting from the aspect of being able to communicate with external programs. Fibers are completely 1.9. There&#8217;s no such thing as fibers in ruby 1.8. They sound like they&#8217;d be some kind [...]]]></description>
			<content:encoded><![CDATA[<p>Fibers, threads, and processes! Oh, my! Fibers are probably the most interesting part of this, and ruby&#8217;s slightly broken threading model. Processes are interesting from the aspect of being able to communicate with external programs.<span id="more-111"></span></p>
<p>Fibers are completely 1.9. There&#8217;s no such thing as fibers in ruby 1.8. They sound like they&#8217;d be some kind of lightweight thread, but are really just a coroutine. I&#8217;ve never heard of coroutine stuff before, so this concept is new to me. It allows you to decouple a few things. The books example is having a background fiber running some complex calculation while the main thread does something with the previous result of that calculation. There&#8217;s some more on fibers, coroutines, and continuations, but the text doesn&#8217;t appear to be very complete on this part. It links to oblivion to get more information on continuations. Doh.</p>
<p>Multithreading. Ruby 1.8&#8242;s threading model was implemented entirely within the interpreter. So you only got one thread on the CPU, one core. Not so great. Ruby 1.9 has some marginal improvements so that you can run native threads, but due to the thread model changing it will never really run more than one thread concurrently. So while it can jump between threads quickly, you can&#8217;t have a truly multithreaded application in ruby. I&#8217;m hoping that ruby 2.0 or maybe 1.9.1 will have updated those libraries that don&#8217;t work in this threading model so that true multithreading can happen. Especially for things like Rails, where it could provide a huge performance benefit.</p>
<p>The exsting threading abilities in ruby are pretty potent. You can get the stats, see if it&#8217;s alive, join with it, and set the priority of the thread. Threading behaves similar to how it works in other languages. Without the debug flag set, a thread that raises an exception will die, but the others will continue. Which is the way things should happen, but it&#8217;s difficult to catch exceptions that way. You can set abort_on_exception to true, and then the vm will abort with a RuntimeError when the thread raises an exception. There is of course a mutex to help with synchronization issues. Finally, there&#8217;s also a Queue class for producer/consumer type applications.</p>
<p>Now you get into running multiple processes. The simplest is to just call system(&#8220;do some command&#8221;) or use backticks `.  If you want to have a small conversation with your external process, you&#8217;ll need to use IO.popen. You can then talk to it and read from it. You may have to flush the data to the IO object. Nothing terribly new to people that have worked with external processes before. Ruby supports a fork(2) type invocation by passing a single minus sign to IO.popen(&#8220;-&#8221;,&#8221;w+&#8221;). Ruby will then fork off another interpreter, the parent will get back the IO object, and the child will get nil. Ruby also supports Kernel.fork, Kernel.exec and IO.pipe, but the book doesn&#8217;t go into it much, except that it&#8217;s platform dependent. Lastly, you can pass a block to it IO.popen. Works amazingly similar to File.open. Block runs, and when it&#8217;s done the popen is cleaned up. A naieve assumption again, however, because it&#8217;s difficult to do any error handling.</p>
<p>Well, that&#8217;s it for today. Next up is Unit testing. Blech. I may be almost ready to go on to ruby on rails. The second part of the book is &#8220;Ruby in its setting.&#8221; Talks about Command-Line arguments, namespaces, character encoding, documenting ruby, ruby and the web, and finally ruby and Microsloth Windows. May have to skim those chapters. It might be time to learn me some rails! And build something!</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2008/07/28/ruby-fibrous-content-ahead/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Input and Output</title>
		<link>http://shlrm.org/wordpress/2008/07/27/ruby-input-and-output/</link>
		<comments>http://shlrm.org/wordpress/2008/07/27/ruby-input-and-output/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 01:20:28 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=104</guid>
		<description><![CDATA[The next chapter is one on basic input and output. This one is also kinda short. Or maybe it feels short, as there&#8217;s not a whole lot of new stuff to digest&#8230; EDIT: heh, well I started out saying it was short, but turns out I had more to say about it than I thought. [...]]]></description>
			<content:encoded><![CDATA[<p>The next chapter is one on basic input and output. This one is also kinda short. Or maybe it feels short, as there&#8217;s not a whole lot of new stuff to digest&#8230; EDIT: heh, well I started out saying it was short, but turns out I had more to say about it than I thought. Maybe I&#8217;m getting better at this blagging stuff.<span id="more-104"></span></p>
<p>An IO object in ruby is a bidirectional channel between ruby and some external resource, to almost quote the text. There&#8217;s a foot note that tells you that a single IO object may have more than one file descriptor. The text talks about how things can automatically close, but poor error handling can cause problems there. The example given is where you pass a block to the File.open method. You process the file within the block and if everything goes well, the file is autmoatically closed when the block ends. However, your file variable is scoped within that block, so if an error is thrown, you lose your file variable. You&#8217;ll end up with an IO object floating around in the heap, keeping files open, until it finally gets collected. Not really a good thing to do.</p>
<p>There&#8217;s a few examples on reading from files. You can read an entire file into a string, or into an array of lines. Kinda handy. Just don&#8217;t forget to rescue exceptions. Writing to files is pretty simple. You can even simulate an iostream from c++ since the &lt;&lt; operator is set up for append in ruby. You can also include &#8216;stringio&#8217; and do all your fancy file operations directly in memory on strings. Handy for when you need to submit stuff via XML-RPC or some sort of HTTP method.</p>
<p>Ruby comes with a set of classes in the socket library so that you can play with TCP, UDP, SOCKS, and Unix sockets. You can also deal with network goodies at a very high level. To thief a simplistic example from the book:</p>
<pre lang="ruby">require 'open-uri'
open('http://pragprog.com') do |f|
  puts f.read.scan(/&lt;img alt=".*?" src="(.*?)"/m).uniq
end
</pre>
<p>That example there shows how painfully easy it is to theif images from a website. Of course, this is a naieve example. We don&#8217;t rescue any errors or anything, but still, it works quite well. The text promises more to come in Chapter 20 about using Ruby on the interblags. I can&#8217;t wait!</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2008/07/27/ruby-input-and-output/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Ruby: First Edition</title>
		<link>http://shlrm.org/wordpress/2008/07/27/programming-ruby-first-edition/</link>
		<comments>http://shlrm.org/wordpress/2008/07/27/programming-ruby-first-edition/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 22:52:16 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=102</guid>
		<description><![CDATA[Is available online! How about that? There&#8217;s a second edition out, and the third edition is soon to head to the presses. I have the third edition PDF, and that&#8217;s what I&#8217;ve been blagging about. But, if you&#8217;re broke (or cheap) and want to learn ruby, I highly reccomend the book.]]></description>
			<content:encoded><![CDATA[<p>Is <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/">available online</a>! How about that? There&#8217;s a second edition out, and the third edition is soon to head to the presses. I have the third edition PDF, and that&#8217;s what I&#8217;ve been blagging about. But, if you&#8217;re broke (or cheap) and want to learn ruby, I highly reccomend the book.</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2008/07/27/programming-ruby-first-edition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quickie on Ruby Exceptions</title>
		<link>http://shlrm.org/wordpress/2008/07/27/quickie-on-ruby-exceptions/</link>
		<comments>http://shlrm.org/wordpress/2008/07/27/quickie-on-ruby-exceptions/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 20:58:37 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[throw catch]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=100</guid>
		<description><![CDATA[I&#8217;m feeling kinda tired, so the chapter on exceptions is gonna be a short one. Turns out I was getting sick. Bleh. Yay for day-quil! Anyway, the chapter in the book on Exceptions is kinda small. Exceptions work just about exactly the same as they do in Java. Throw and catch aren&#8217;t quite the same, [...]]]></description>
			<content:encoded><![CDATA[<p><span style="text-decoration: line-through;">I&#8217;m feeling kinda tired, so the chapter on exceptions is gonna be a short one.</span></p>
<p>Turns out I was getting sick. Bleh. Yay for day-quil! Anyway, the chapter in the book on Exceptions is kinda small. Exceptions work just about exactly the same as they do in Java. Throw and catch aren&#8217;t quite the same, however. In ruby you &#8220;fail&#8221; or &#8220;raise&#8221; an exception. &#8220;throw&#8221; and &#8220;catch&#8221; work for bailing out of loops.</p>
<p>In ruby, exceptions are caught by using &#8220;rescue&#8221; and behave just like catch in Java. There is also a &#8220;finally&#8221; type clause, called &#8220;ensure&#8221;; behaves the exact same way it does in Java. Ruby has the concept of a &#8220;retry&#8221; which is kinda neat. There is great potential for infinite loops, however, so take care. &#8220;raise&#8221; in ruby behaves the same way as &#8220;throw&#8221; in java.</p>
<p>&#8220;catch&#8221; and &#8220;throw&#8221; are different. You use it to bail from a loop. Ruby will crawl up the stack when you throw something, looking for the location to catch it. Then it&#8217;ll jump outside that loop and finish. I personally think that&#8217;s pretty spagetti code inducing, but I can see where it might have its usefulness.</p>
<p>And that&#8217;s it. This blag post is really almost as long as the actual chapter. Next up is I/O.</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2008/07/27/quickie-on-ruby-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby: Friday Facets</title>
		<link>http://shlrm.org/wordpress/2008/07/25/ruby-friday-facets/</link>
		<comments>http://shlrm.org/wordpress/2008/07/25/ruby-friday-facets/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 18:15:11 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=98</guid>
		<description><![CDATA[How about that: &#8220;Facets&#8221; of ruby. I&#8217;m so witty, it hurts. I&#8217;ll quickly go over methods and a sentence or two on expressions. Parallel assignments get a few more words, since it&#8217;s new. A bit on boolean logic, and case statements, because they&#8217;re cool. Finishing up with loops and a few words on scoping. The book discusses [...]]]></description>
			<content:encoded><![CDATA[<p>How about that: &#8220;Facets&#8221; of ruby. I&#8217;m so witty, it hurts. I&#8217;ll quickly go over methods and a sentence or two on expressions. Parallel assignments get a few more words, since it&#8217;s new. A bit on boolean logic, and case statements, because they&#8217;re cool. Finishing up with loops and a few words on scoping.<span id="more-98"></span></p>
<p>The book discusses defining and calling methods. This is a very fascinating topic, not. They only new thing there for me was &#8220;splat&#8221; args. The concept of rolling up all the arguments into one array and then you can hand that on to the super, or another method. The *args will get automatically expanded into parameters for the other method.</p>
<p>More things are expressions in ruby than in other languages. If statements aren&#8217;t statements, they&#8217;re expressions. If returns a value.</p>
<p>Parallel assignment is another new concept. Wierd, but useful. It makes it easy to swap variables. You don&#8217;t have to define a swap variable to switch things out.  Nested parallel assignment travels down the dark rabbit hole of insanity. It&#8217;s ugly to look at, and difficult to comprehend. You can use splats to eat variables. You can have a single splat anywhere in the assignment (a, *b, c) and it&#8217;ll eat everything as long as the rest of the variables are satisifed. So you could eat everything in the middle, and just have the first and last ones assigned. I&#8217;m not really sure where this will be useful, but it&#8217;s there. Finally, there&#8217;s no ++ or &#8212; operator in ruby! You have to use += 1.</p>
<p>Theres the standard gamut of boolean operators, adding a few. &amp;&amp; and &#8220;and&#8221; do short-circuit logic, so that&#8217;s nice. However, &#8221;and&#8221; has a lower precedence than &amp;&amp;. To mix it up even more, &#8220;and&#8221; and &#8220;or&#8221; have the same precedence but &amp;&amp; has a higher precedence than ||. I believe that could cause some really wierd problems for people expecting a certain behavior. I think I&#8217;ll stick to just using &amp;&amp; and ||. There&#8217;s a construct for setting a value to a variable if it doesn&#8217;t exist: var ||= &#8220;default value&#8221;. Makes it easier than using an if statement. Flip-flops were mentioned again using conditionals and ranges. Perhaps if someone requests it, I&#8217;ll try to explain it better, but I think I understand the concept well enough and don&#8217;t really need to go into it again.</p>
<p>Case statements are far more powerful in ruby. You don&#8217;t have to have a simple type. Since everything is an object, case uses the method ===. That makes it really awesome. You can have a case statement doing regular expression evaluations to parse lines. Quite handy. You can attach a conditional to a statement to only run that statement if $foo evaluates to true. Reads nice and clean, however you can quickly descend into madness if you&#8217;re not careful. You can have if expressions that return a value only if $foo. So yeah.</p>
<pre lang="ruby">y = if even then
  6
else
  5
end if assign_y == true</pre>
<p>According to the text, ruby has lame built-in looping constructs, because iterators handle the majority of looping! You&#8217;ve got the standard while and until construct. You can attach while and until to a block, but the block will always be run at least once, regardless of the evaluation of the while or until. That&#8217;s not quite normal, especially since it won&#8217;t run a statement once. There&#8217;s &#8220;next&#8221;, &#8220;break&#8221;, and &#8220;redo&#8221; modifiers to manipulate flow of the loop. Redo is quite handy, makes it simpler than using recursion to reprocess a line. For .. in constructs are neat, basically it&#8217;s a cleaner way to write iterator |paramter| block code. It has a bit of scoping difference, so I&#8217;ll probably try to stick to the more explicit syntax. </p>
<p>A bit on scoping. Ruby 1.9 has a concept of block local variables, so you can define a variable that is scope limited to a block. The example in the book shows it pretty well, you have a variable called square, defined outside the block. You then use a variable square within a block, with a different purpose. Now my variable outside the block is broken, because the scope leaked. You can prevent this in ruby 1.9 by |; square| as a block parameter. It&#8217;s now local to that scope and your external variable is unaffected. Yeah, you could just use a different name, but this improves readability. At least, I think it does, my brain can handle keeping scope. Turning on ruby warnings (which by the way is appearing to be a good thing to do) will help you catch these scope issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2008/07/25/ruby-friday-facets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
