<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>rors.org</title>
    <link>http://rors.org</link>
    <description>rors.org - Blog posts</description>
    <language>en</language>
    <item>
      <title>Non-English resourceful routes </title>
      <description>&lt;p&gt;I &lt;a href="http://rors.org/2008/3/26/map-resources-x-as-y"&gt;previously wrote&lt;/a&gt; about resourceful routes improvement in &lt;strong&gt;edge&lt;/strong&gt; that will provide URLs independent of resources path names in application. This is a great benefit for Non-English URLs.&lt;/p&gt;


	&lt;p&gt;But that didn&amp;#8217;t solved &amp;#8216;new&amp;#8217; and &amp;#8216;edit&amp;#8217; parts of &lt;span class="caps"&gt;URL&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Finally &lt;a href="http://github.com/rails/rails/commit/063c393bf0a2eb762770c97f925b7c2867361ad4"&gt;this improvement&lt;/a&gt; and &lt;a href="http://github.com/rails/rails/commit/e6a3ce3392812f707b78d64ffb04ee52f4517d20"&gt;bugfix follow-up&lt;/a&gt;     did just that:&lt;/p&gt;


&lt;pre&gt;
map.resources :cars, 
              :as =&amp;gt; 'kola', 
              :path_names =&amp;gt; { :new =&amp;gt; 'novo', 
                               :edit =&amp;gt; 'izmena' }
&lt;/pre&gt;

	&lt;p&gt;...so that:&lt;br /&gt;&lt;pre&gt;
new_car_path      # =&amp;gt; /kola/nova 
edit_car_path(17) # =&amp;gt; /kola/17/izmena
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;...will map nicely to appropriate controller actions.&lt;/p&gt;


	&lt;p&gt;To globally set new values use:&lt;br /&gt;&lt;pre&gt;
ActionController::Base.resources_path_names = { 
  :new =&amp;gt; 'novo', 
  :edit =&amp;gt; 'izmena' }
&lt;/pre&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 24 Apr 2008 00:00:00 -0400</pubDate>
      <author>Dejan Simic</author>
      <link>http://rors.org/2008/4/24/non-english-resourceful-routes</link>
      <guid>http://rors.org/2008/4/24/non-english-resourceful-routes</guid>
    </item>
    <item>
      <title>map.resources :x, :as =&gt; 'y'</title>
      <description>&lt;p&gt;Somehow I&amp;#8217;ve missed this &lt;a href="http://dev.rubyonrails.org/changeset/8785"&gt;two months old improvement&lt;/a&gt;,  that will mean a lot for non-english Rails apps.&lt;/p&gt;


	&lt;p&gt;From its creation, &lt;a href="http://api.rubyonrails.org/classes/ActionController/Resources.html"&gt;Resources&lt;/a&gt; had a convention &lt;strong&gt;without&lt;/strong&gt; configuration for resource route names and &lt;span class="caps"&gt;URL&lt;/span&gt;. One determines another.&lt;/p&gt;


	&lt;p&gt;Finally, Rails edge (set to be in Rails 2.1) has this feature:&lt;/p&gt;


&lt;pre&gt;
map.resources :comments, :as =&amp;gt; 'komentari'
&lt;/pre&gt;

	&lt;p&gt;This will give you restful route names in English (preferred for code readability), while letting you customize resource name in &lt;span class="caps"&gt;URL&lt;/span&gt; (ie. /komentari/17).&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;&lt;/strong&gt;: This article has a &lt;a href="http://rors.org/2008/4/24/non-english-resourceful-routes"&gt;follow-up&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Wed, 26 Mar 2008 00:00:00 -0400</pubDate>
      <author>Dejan Simic</author>
      <link>http://rors.org/2008/3/26/map-resources-x-as-y</link>
      <guid>http://rors.org/2008/3/26/map-resources-x-as-y</guid>
    </item>
    <item>
      <title>Self or default if empty, DRY way [6]</title>
      <description>&lt;p&gt;I really appreciate following idiom in Ruby:&lt;br /&gt;&lt;pre&gt;
person.phone || "N/A" 
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s &lt;span class="caps"&gt;DRY&lt;/span&gt;. No need for conditional structures, and repeating person.phone in condition and as return value.&lt;/p&gt;


	&lt;p&gt;But lets say you don&amp;#8217;t control your data source, and there is an empty string instead of nil when data is not available (sounds familiar?). How would you deal with that and keep things simple and &lt;span class="caps"&gt;DRY&lt;/span&gt;?&lt;/p&gt;


	&lt;p&gt;Well, basicly all you need is a String method that will return nil if string is empty, or self if it&amp;#8217;s not empty. String object doesn&amp;#8217;t have that kind of method, at least not that specialized. Regexp to rescue. Lets try this:&lt;br /&gt;&lt;pre&gt;
&amp;gt;&amp;gt; ""[/.+/m]
=&amp;gt; nil

&amp;gt;&amp;gt; "ruby"[/.+/m]
=&amp;gt; "ruby" 
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;Nice. So solution for the problem above is:&lt;/p&gt;


&lt;pre&gt;
person.phone[/.+/m] || "N/A" 
&lt;/pre&gt;

	&lt;p&gt;Throw &lt;a href="http://rors.org/2008/3/18/andand"&gt;andand&lt;/a&gt; in game to support both nil and empty value with no cost in complexity:&lt;br /&gt;&lt;pre&gt;
person.phone.andand[/.+/m] || "N/A" 
&lt;/pre&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 19 Mar 2008 00:00:00 -0400</pubDate>
      <author>Dejan Simic</author>
      <link>http://rors.org/2008/3/19/self-or-default-if-empty-dry-way</link>
      <guid>http://rors.org/2008/3/19/self-or-default-if-empty-dry-way</guid>
    </item>
    <item>
      <title>andand [2]</title>
      <description>&lt;p&gt;I really, really like &lt;a href="http://andand.rubyforge.org/"&gt;andand&lt;/a&gt; . Try it and stop repeating yourself.&lt;/p&gt;


	&lt;p&gt;I mean, why would you want to write this:&lt;br /&gt;&lt;pre&gt;
entry.at('description') &amp;#38;&amp;#38; entry.at('description').inner_text
&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;when you can write this:&lt;br /&gt;&lt;pre&gt;
entry.at('description').andand.inner_text
&lt;/pre&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 18 Mar 2008 00:00:00 -0400</pubDate>
      <author>Dejan Simic</author>
      <link>http://rors.org/2008/3/18/andand</link>
      <guid>http://rors.org/2008/3/18/andand</guid>
    </item>
    <item>
      <title>Eleventh [5]</title>
      <description>&lt;p&gt;Did you know that Ruby was Matz&amp;#8217; eleventh take on creating a programming language? &lt;a href="http://podcast.rubyonrails.org/system/audio/2006/rails-040-Yukihiro_Matz_Matsumoto.mp3"&gt;Listen&lt;/a&gt;. So don&amp;#8217;t despare next time you fail, you are right on track.&lt;/p&gt;


	&lt;p&gt;&amp;#8220;I sat down and counted the other day and Ruby was eleventh language I&amp;#8217;ve tried to write. Out of those 4 only had names, 3 of them didn&amp;#8217;t work at all&amp;#8230; the rest of them was just experiments. Ruby is the only one that really worked, so I guess that makes Ruby the first one, but it was one out of eleven attempts.&amp;#8221; ~ Matz&lt;/p&gt;


&lt;pre&gt;
The road to wisdom? -- Well, it's plain
and simple to express:
           Err
           and err
           and err again
           but less
           and less
           and less.

~Piet Hein
&lt;/pre&gt;</description>
      <pubDate>Mon, 11 Feb 2008 00:00:00 -0500</pubDate>
      <author>Dejan Simic</author>
      <link>http://rors.org/2008/2/11/eleventh</link>
      <guid>http://rors.org/2008/2/11/eleventh</guid>
    </item>
    <item>
      <title>Nested Ternary operator</title>
      <description>&lt;p&gt;I was going through some of my old Java code and found this interesting piece of code. It&amp;#8217;s basically alternative way of creating switch structure.&lt;/p&gt;


&lt;pre&gt;
public Object getValueAt(int row, int col) {
  Person person = data.get(row);
  return 
    col==0? person.getFirstName() 
    : col==1? person.getLastName() 
    : col==2? person.getAge()            
    : col==3? person.getAddress()
    : null; 
}
&lt;/pre&gt;

Cons:
	&lt;ul&gt;
	&lt;li&gt;unlike switch, there is no compile time error for duplicated cases&lt;/li&gt;
		&lt;li&gt;this is not a standard way of dealing with different values of a variable so you could leave wondering the person that will read you code&lt;/li&gt;
	&lt;/ul&gt;


Pros:
	&lt;ul&gt;
	&lt;li&gt;more control over branching logic since expressions are used (switch must use constants as case values)&lt;/li&gt;
		&lt;li&gt;this is not a standard way of dealing with different values of a variable so you could be seen as hacker in the eyes of the person that will read you code ;)&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Sun, 20 Jan 2008 00:00:00 -0500</pubDate>
      <author>Dejan Simic</author>
      <link>http://rors.org/2008/1/20/nested-ternary-operator</link>
      <guid>http://rors.org/2008/1/20/nested-ternary-operator</guid>
    </item>
    <item>
      <title>Hello World [1]</title>
      <description>&lt;p&gt;Well what title of first post did you expect from a programmer? :)&lt;/p&gt;</description>
      <pubDate>Mon, 14 Jan 2008 00:00:00 -0500</pubDate>
      <author>Dejan Simic</author>
      <link>http://rors.org/2008/1/14/hello-world</link>
      <guid>http://rors.org/2008/1/14/hello-world</guid>
    </item>
  </channel>
</rss>
