<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4124821</atom:id><lastBuildDate>Wed, 28 Aug 2024 11:43:13 +0000</lastBuildDate><category>Programming Tips</category><category>Ruby</category><title>Bhanu&#39;s Thoughts</title><description>Tilting at windmills...</description><link>http://bhanu.blogspot.com/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4124821.post-4296308908695765383</guid><pubDate>Sat, 31 Mar 2007 03:38:00 +0000</pubDate><atom:updated>2007-04-02T22:26:10.567-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Programming Tips</category><category domain="http://www.blogger.com/atom/ns#">Ruby</category><title>Gotcha with Ruby &quot;upcase!&quot; and &quot;downcase!&quot;</title><description>Ruby String class provides a method called &lt;code&gt;upcase&lt;/code&gt; that converts all characters of a string to upper case. Ruby also provides an equivalent (&lt;code&gt;downcase&lt;/code&gt;) to convert all the characters of the string to lower case. The return value of &lt;code&gt;upcase&lt;/code&gt; or &lt;code&gt;downcase&lt;/code&gt; is a copy of the string with all the characters converted to upper or lower case. To convert the string in place, there are &lt;code&gt;upcase!&lt;/code&gt; and &lt;code&gt;downcase!&lt;/code&gt;. The gotcha comes from these two methods. If a string (say, &quot;HELLO WORLD&quot;) is all upper case, then invoking &lt;code&gt;upcase!&lt;/code&gt; on that string returns nil. This is different from how &lt;code&gt;upcase&lt;/code&gt; and &lt;code&gt;downcase&lt;/code&gt; function - they return a copy of the string even if all the characters are upper case. This could be a big gotcha!&lt;br /&gt;&lt;br /&gt;Examples -&lt;br /&gt;&lt;br /&gt;&lt;code&gt;my_string = &quot;Hello World&quot;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;my_string1 = &quot;HELLO WORLD&quot;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;ret_val = my_string.upcase&lt;/code&gt;&lt;br /&gt;&lt;code&gt;ret_val1 = my_string1.upcase&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Both &lt;code&gt;ret_val&lt;/code&gt; and &lt;code&gt;ret_val1&lt;/code&gt; are &quot;&lt;code&gt;HELLO WORLD&lt;/code&gt;&quot;.&lt;br /&gt;&lt;br /&gt;Now, consider the following -&lt;br /&gt;&lt;br /&gt;&lt;code&gt;gotcha_string = &quot;Hello World&quot;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;gotcha_string1 = &quot;HELLO WORLD&quot;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;ret_val = gotcha_string.upcase!&lt;/code&gt;&lt;br /&gt;&lt;code&gt;ret_val1 = gotcha_string1.upcase!&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here, &lt;code&gt;ret_val&lt;/code&gt; is &quot;&lt;code&gt;HELLO WORLD&lt;/code&gt;&quot; whereas &lt;code&gt;ret_val1&lt;/code&gt; is &quot;&lt;code&gt;nil&lt;/code&gt;&quot;.&lt;br /&gt;&lt;br /&gt;We would expect &lt;code&gt;ret_val1&lt;/code&gt; to also be &quot;&lt;code&gt;HELLO WORLD&lt;/code&gt;&quot;. So, if we used the bang versions of &lt;code&gt;upcase&lt;/code&gt; and &lt;code&gt;downcase&lt;/code&gt; like we would use the non-bang versions, we would be in for a rude surprise.&lt;br /&gt;&lt;br /&gt;In my opinion, this violates the &lt;em&gt;Principle of Least Surprise&lt;/em&gt; that Ruby supposedly adheres to.&lt;br /&gt;&lt;br /&gt;&lt;em&gt; &lt;strong&gt; Update &lt;/strong&gt; &lt;/em&gt;&lt;br /&gt;I just want to clarify that the value of &lt;code&gt;gotcha_string1&lt;/code&gt; itself doesn&#39;t become nil, just the return value of calling that variable is nil. &lt;code&gt;gotcha_string1&lt;/code&gt; will still be &quot;&lt;code&gt;HELLO WORLD&lt;/code&gt;&quot;. Its return value which is assigned to &lt;code&gt;ret_val1&lt;/code&gt; will be nil. As the commenter below suggests, do not use the &quot;!&quot; version of &lt;code&gt;upcase&lt;/code&gt; or &lt;code&gt;downcase&lt;/code&gt; in an assignment or do not depend on their return value.&lt;br /&gt;&lt;br /&gt;&lt;script type=&quot;text/javascript&quot;&gt;&lt;br /&gt;digg_url = &#39;http://bhanu.blogspot.com/2007/03/gotcha-with-upcase-and-downcase.html&#39;;&lt;br /&gt;digg_title = &#39;Gotcha with Ruby &quot;upcase!&quot; and &quot;downcase!&quot; &#39;;&lt;br /&gt;digg_bodytext = &#39;In Ruby, upcase! and downcase! don&#39;t function similarly to upcase and downcase in case the string already contains all upper case or lower case characters. This post shows what the gotcha is.&#39;;&lt;br /&gt;digg_topic = &#39;programming&#39;;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;script src=&quot;http://digg.com/tools/diggthis.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;script src=&quot;http://www.google-analytics.com/urchin.js&quot; type=&quot;text/javascript&quot;&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;script type=&quot;text/javascript&quot;&gt;&lt;br /&gt;_uacct = &quot;UA-1595086-1&quot;;&lt;br /&gt;urchinTracker();&lt;br /&gt;&lt;/script&gt;</description><link>http://bhanu.blogspot.com/2007/03/gotcha-with-upcase-and-downcase.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>9</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4124821.post-6294686557695227253</guid><pubDate>Mon, 26 Mar 2007 23:00:00 +0000</pubDate><atom:updated>2007-03-27T22:49:59.072-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Programming Tips</category><category domain="http://www.blogger.com/atom/ns#">Ruby</category><title>Capitalizing The First Letter Of Each Word In A String</title><description>Say we have a string &quot;This is my String&quot; - we want to capitalize the first letter of each word so that my string becomes &quot;This Is My String&quot;. My first attempt was to use the &lt;code&gt; capitalize &lt;/code&gt; method -&lt;br /&gt;&lt;br /&gt;&lt;code&gt; my_string = &quot;This is a String&quot; &lt;/code&gt;&lt;br /&gt;&lt;code&gt; my_string.capitalize --&gt; &quot;This is a string&quot; &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Not exactly what we want. Turns out that &lt;code&gt; capitalize &lt;/code&gt; converts the *first character* of the string (letter &quot;T&quot; in this case) to upper case and the rest of the letter to lower case. In other words, if&lt;br /&gt;&lt;br /&gt;&lt;code&gt; my_string1 = &quot;this is a String&quot;, &lt;/code&gt;&lt;br /&gt;&lt;code&gt; my_string1.capitalize --&gt; &quot;This is a string&quot; &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Notice that &quot;t&quot; in &quot;this&quot; became &quot;T&quot; and &quot;S&quot; in &quot;string&quot; became &quot;s&quot;.&lt;br /&gt;&lt;br /&gt;In order to capitalize the first letter of each word, we have to split the string into words and then apply capitalize on each word.&lt;br /&gt;&lt;br /&gt;&lt;code&gt; my_string.split(” “).each{|word| word.capitalize!}.join(” “) --&gt; &quot;This Is A String&quot; &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We used &lt;code&gt; &quot;capitalize!&quot; &lt;/code&gt; instead of &lt;code&gt; &quot;capitalize&quot; &lt;/code&gt;. The difference is that &lt;b&gt; &lt;code&gt; &quot;capitalize!&quot; &lt;/code&gt; &lt;/b&gt; capitalizes the word in place whereas  &lt;b&gt; &lt;code&gt; &quot;capitalize&quot; &lt;/code&gt; &lt;/b&gt; returns a copy of the word capitalized.&lt;br /&gt;&lt;br /&gt;What if we wanted to capitalize all the letters in the string? Turns out, this is straight-forward:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;my_string.upcase!  --&gt; &quot;THIS IS A STRING&quot;&lt;/code&gt;</description><link>http://bhanu.blogspot.com/2007/03/capitalizing-first-letter-of-each-word.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4124821.post-3141303781768181743</guid><pubDate>Mon, 26 Mar 2007 22:40:00 +0000</pubDate><atom:updated>2007-03-26T15:59:59.912-07:00</atom:updated><title>First Post</title><description>First post...getting started. I will try to post about Ruby, Rails and JRuby as and when I find the time.</description><link>http://bhanu.blogspot.com/2007/03/first-post.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>1</thr:total></item></channel></rss>