<?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>ph-lee &#187; rails</title>
	<atom:link href="http://www.ph-lee.com/tag/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ph-lee.com</link>
	<description>aspire to inspire before you expire</description>
	<lastBuildDate>Sat, 29 May 2010 19:11:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Installing mysql on Mac Snow Leopard</title>
		<link>http://www.ph-lee.com/2009/09/18/installing-mysql-on-mac-snow-leopard/</link>
		<comments>http://www.ph-lee.com/2009/09/18/installing-mysql-on-mac-snow-leopard/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 17:11:59 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database.yml]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[snow leopard]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=197</guid>
		<description><![CDATA[This articles is regarding Mac OS X 10.6 with mysql 5.1.37.
I followed this article at hivelogic.com which required compiling mysql from source. It mostly worked with a few hitches which I&#8217;d like to share.
After following the article and attempting to run my rails app the first problem I hit was&#8230;
uninitialized constant MysqlCompat::MysqlRes
this problem seemed to [...]]]></description>
			<content:encoded><![CDATA[<p>This articles is regarding Mac OS X 10.6 with mysql 5.1.37.</p>
<p>I followed <a href="http://hivelogic.com/articles/compiling-mysql-on-snow-leopard/">this article at hivelogic.com</a> which required compiling mysql from source. It mostly worked with a few hitches which I&#8217;d like to share.</p>
<p>After following the article and attempting to run my rails app the first problem I hit was&#8230;</p>
<p><code>uninitialized constant MysqlCompat::MysqlRes</code></p>
<p>this problem seemed to be that you need to specify the atchitecture as 64-bit when installing the mysql gem. So run the below command intead when install the mysql gem&#8230;</p>
<p><code>sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-<br />
config=/usr/local/mysql/bin/mysql_config</code></p>
<p>Or if you already installed the gem just re-install with this command. The next problem was the following&#8230;</p>
<p><code>Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)</code></p>
<p>I&#8217;d move a project from ubuntu onto the mac I couldn&#8217;t find the mysql socket to use as it was specifying the wrong location. On the mac the file can be found at</p>
<p><code>/tmp/mysql.sock</code></p>
<p>So you&#8217;ll need to modify your database.yml file accordingly to point to this location</p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/09/18/installing-mysql-on-mac-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Gotcha : Mysql::Error: Unknown database &#039;application_name_test&#039;  /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize&#039;</title>
		<link>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-unknown-database-application_name_test-usrlibruby18monitorrb242in-synchronize/</link>
		<comments>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-unknown-database-application_name_test-usrlibruby18monitorrb242in-synchronize/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 18:03:11 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=146</guid>
		<description><![CDATA[After dealing with thisGotcha : Mysql::Error: Access denied for user ‘root’@&#8217;localhost’ you&#8217;ve hit another problem of&#8230;
This means rails is looking for the test database but can&#8217;t find it cause it&#8217;s not there. Simply got to mysql Query Browser or command line, whichever you prefer and create a new database/schema of your application_name followed by _test [...]]]></description>
			<content:encoded><![CDATA[<p>After dealing with this<a href="http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-access-denied-for-user-rootlocalhost-using-password-no-usrlibruby18monitorrb242in-synchronize/">Gotcha : Mysql::Error: Access denied for user ‘root’@&#8217;localhost’</a> you&#8217;ve hit another problem of&#8230;</p>
<script src="http://gist.github.com/149984.js"></script>
<p>This means rails is looking for the test database but can&#8217;t find it cause it&#8217;s not there. Simply got to mysql Query Browser or command line, whichever you prefer and create a new database/schema of your application_name followed by _test so for example</p>
<p><code>my_app_test</code></p>
<p>Theres no need to copy/clone the database structure a across. Rails will replicate the development database as the test database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-unknown-database-application_name_test-usrlibruby18monitorrb242in-synchronize/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gotcha : Mysql::Error: Access denied for user &#039;root&#039;@&#039;localhost&#039; (using password: NO) /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize&#039;</title>
		<link>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-access-denied-for-user-rootlocalhost-using-password-no-usrlibruby18monitorrb242in-synchronize/</link>
		<comments>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-access-denied-for-user-rootlocalhost-using-password-no-usrlibruby18monitorrb242in-synchronize/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 17:50:47 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[database.yml]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=141</guid>
		<description><![CDATA[So you decided to run some tests on your new rails project and this error crops up&#8230;
Looks like you forget to configure the test database parameters in the configuration/database.yml file.
]]></description>
			<content:encoded><![CDATA[<p>So you decided to run some tests on your new rails project and this error crops up&#8230;</p>
<script src="http://gist.github.com/149980.js"></script>
<p>Looks like you forget to configure the test database parameters in the configuration/database.yml file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/19/gotcha-mysqlerror-access-denied-for-user-rootlocalhost-using-password-no-usrlibruby18monitorrb242in-synchronize/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>(Ruby on rails) Adding an admin area to your web application</title>
		<link>http://www.ph-lee.com/2009/07/18/ruby-on-rails-adding-an-admin-area-to-your-application/</link>
		<comments>http://www.ph-lee.com/2009/07/18/ruby-on-rails-adding-an-admin-area-to-your-application/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 15:48:05 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=137</guid>
		<description><![CDATA[Most web applications have an admin area and a user area. Blogs, webstores, social sites pretty much any dynamic site. You&#8217;ll want an admin area to do all the backend stuff to manage all your content and users. [icebergist] gives one way of adding an admin area to your web app.
I&#8217;ve tried this method and [...]]]></description>
			<content:encoded><![CDATA[<p>Most web applications have an admin area and a user area. Blogs, webstores, social sites pretty much any dynamic site. You&#8217;ll want an admin area to do all the backend stuff to manage all your content and users. <a href="http://icebergist.com/">[icebergist]</a> gives <a href="http://icebergist.com/posts/restful-admin-namespaced-controller-using-scaffolding">one way of adding an admin area</a> to your web app.</p>
<p>I&#8217;ve tried this method and so far it seems nice. It completely separates the two area giving you full control over each.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/18/ruby-on-rails-adding-an-admin-area-to-your-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to : Exceptions in ruby (on rails) coming from a Java or C# .NET background</title>
		<link>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/</link>
		<comments>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 13:25:54 +0000</pubDate>
		<dc:creator>ph-lee</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[begin]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[catch]]></category>
		<category><![CDATA[end]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[raise]]></category>
		<category><![CDATA[rescue]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[throw]]></category>
		<category><![CDATA[try]]></category>

		<guid isPermaLink="false">http://www.ph-lee.com/?p=114</guid>
		<description><![CDATA[Personally coming from a Java and C# .NET background exceptions in ruby (on rails) seemed primative and rather lacking compared to the former languages I have had experiences in. To show off the differences and to show how to do exceptions in ruby lets start of with some examples.
C# .NET Example
Class (User)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/// &#60;summary&#62;
/// constructor for [...]]]></description>
			<content:encoded><![CDATA[<p>Personally coming from a Java and C# .NET background exceptions in ruby (on rails) seemed primative and rather lacking compared to the former languages I have had experiences in. To show off the differences and to show how to do exceptions in ruby lets start of with some examples.</p>
<p>C# .NET Example<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// constructor for creating a new user</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;str_username&quot;&gt;username of the user&lt;/param&gt;</span>
&nbsp;
<span style="color: #0600FF;">public</span> User<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> str_username<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//check method parameters/arguments for null or empty</span>
&nbsp;
   <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>str_username<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//throw an exception since parameters entered are not valid</span>
      <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;argument str_username was null or empty&quot;</span>, <span style="color: #666666;">&quot;str_username&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">str_username</span> <span style="color: #008000;">=</span> str_username<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// absurd method for the purpose of this example</span>
<span style="color: #008080; font-style: italic;">/// creates two users</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> createBobAndEmpty<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//try and create bob</span>
&nbsp;
   <span style="color: #0600FF;">try</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008000;">new</span> User<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Bob&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ArgumentException e<span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #008080; font-style: italic;">//try and create empty</span>
&nbsp;
   <span style="color: #0600FF;">try</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008000;">new</span> User<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ArgumentException e<span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this exception will be thrown in this case</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">//e is the variable storing the exception which we can print the exception message, variable of concern</span>
      <span style="color: #008080; font-style: italic;">//in other cases the entire stack trace can be printed for debugging purposes</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">//in this particular case it would be best to log the error and simply notify the user they can't create a user with empty string as username</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">finally</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//this finally block is optional and normally used for garbage collection/closing connections/closing files etc</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Java Example<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * constructor for creating a new User object
 *
 * @param str_username the username of the user
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> User<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str_username<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//check parameter is not null</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>str_username <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NullPointerException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str_username is null&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//check parameter is not an empty string</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>str_username.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;str_username is empty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">str_username</span> <span style="color: #339933;">=</span> str_username<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * absurd method for the purpose of this example
 * creates 2 users
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> createBobAndEmpty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">//try and create the user bob</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">try</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bob&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NullPointerException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalArgumentException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//try and create user empty</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">try</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NullPointerException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will not be thrown in this case</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IllegalArgumentException</span> e<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this exception will be thrown in this case</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//e is the variable storing the exception which we can print the exception message, variable of concern</span>
      <span style="color: #666666; font-style: italic;">//in other cases the entire stack trace can be printed for debugging purposes</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//in this particular case it would be best to log the error and simply notify the user they can't create a user with empty string as username</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">finally</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//this finally block is optional and normally used for garbage collection/closing connections/closing files etc</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ruby</p>
<p>Firstly Ruby uses different terms/keywords compared to Java or C#, the usual try/catch/throw/finally are different. Instead&#8230;</p>
<ul>
<li>&#8220;try&#8221; becomes &#8220;begin&#8221;</li>
<li>&#8220;catch&#8221; becomes &#8220;rescue&#8221;</li>
<li>&#8220;finally&#8221; becomes &#8220;ensure&#8221;</li>
</ul>
<p>So in Ruby<br />
Class (User)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#class constructor for a user</span>
<span style="color:#008000; font-style:italic;">#username represents the username for the new user</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>username<span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#008000; font-style:italic;">#check argument username is not an empty string</span>
   <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#9966CC; font-weight:bold;">if</span> username.<span style="color:#9900CC;">empty</span>? <span style="color:#008000; font-style:italic;">#keyword &quot;raised&quot; used instead of &quot;throw&quot;</span>
&nbsp;
   <span style="color:#0066ff; font-weight:bold;">@username</span> = username
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#absurd method for the purpose of this article</span>
<span style="color:#008000; font-style:italic;">#create 2 users</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> User.<span style="color:#9900CC;">create_bob_and_empty</span>
   <span style="color:#008000; font-style:italic;">#try and create user user Bob</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">begin</span> <span style="color:#008000; font-style:italic;">#this keyword is used instead of &quot;try&quot;</span>
      User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Bob&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#008000; font-style:italic;">#this rescue clause will not be performed in this example</span>
   <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#008000; font-style:italic;">#this clause will be performed since none of the above rescue clauses were fired</span>
      <span style="color:#008000; font-style:italic;">#can be used to report success of operations where no exceptions were encountered</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
   <span style="color:#008000; font-style:italic;">#try and create user empty</span>
&nbsp;
   <span style="color:#9966CC; font-weight:bold;">begin</span>
      User.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Bob&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#008000; font-style:italic;">#this clause will be fired in this case</span>
&nbsp;
      <span style="color:#008000; font-style:italic;">#the variable e holds the exception that has been raised and can be used to trace exceptions stack</span>
   <span style="color:#9966CC; font-weight:bold;">ensure</span>
      <span style="color:#008000; font-style:italic;">#this clause will be fired in this case</span>
      <span style="color:#008000; font-style:italic;">#this is equivalent to the finally cluse in java or csharp</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>So that completes the examples, now you can compare the differences between ruby and the other languages. The <a href="http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy">ruby exception hierarchy [Sieger]</a> in comparisonm to Java and C# is rather small and limited and it is likely that you will need to <a href="http://rpheath.com/posts/237-raising-custom-exceptions-in-rails">create your own exceptions [Heath]</a> by extending the hierarchy in your application. Before reading Heath&#8217;s article I should warn you his examples are poor as he uses exception for normal control-flow of logic rather than using exception for error handling which what it should only be used for.</p>
<p>Another thing you may also find useful is the keyword &#8220;retry&#8221;. This word/statement can be put in any rescue clause to restart the begin/end block which in lies in. This maybe useful to re-attempt to parse a file or connect to databse etc.</p>
<p>In ruby on top of begin/raise/rescue/end, throw/catch can also be used. Here&#8217;s an example&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">catch</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:done</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
   a = <span style="color:#006666;">1</span>
   <span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#0000FF; font-weight:bold;">true</span>
      throw <span style="color:#ff3333; font-weight:bold;">:done</span> <span style="color:#9966CC; font-weight:bold;">if</span> a == <span style="color:#006666;">100</span>
      a<span style="color:#006600; font-weight:bold;">++</span>
   <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>In ruby, throw does not throw an exception instead it throws a string or :symbol. So as far I can tell it is only used for control-flow purposes rather than the classical case of exception handling, unless someone can tell me otherwise.</p>
<p>Hope you found this article useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ph-lee.com/2009/07/14/how-to-exceptions-in-ruby-on-rails-coming-from-a-java-or-c-net-background/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
