<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4124821</id><updated>2011-09-14T01:23:26.367-07:00</updated><category term='Ruby'/><category term='Programming Tips'/><title type='text'>Bhanu's Thoughts</title><subtitle type='html'>Tilting at windmills...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bhanu.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4124821/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bhanu.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Morampudi Bhanu</name><uri>http://www.blogger.com/profile/03437362908746969144</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4124821.post-4296308908695765383</id><published>2007-03-30T20:38:00.000-07:00</published><updated>2007-04-02T22:26:10.567-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming Tips'/><title type='text'>Gotcha with Ruby "upcase!" and "downcase!"</title><content type='html'>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, "HELLO WORLD") 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 = "Hello World"&lt;/code&gt;&lt;br /&gt;&lt;code&gt;my_string1 = "HELLO WORLD"&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 "&lt;code&gt;HELLO WORLD&lt;/code&gt;".&lt;br /&gt;&lt;br /&gt;Now, consider the following -&lt;br /&gt;&lt;br /&gt;&lt;code&gt;gotcha_string = "Hello World"&lt;/code&gt;&lt;br /&gt;&lt;code&gt;gotcha_string1 = "HELLO WORLD"&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 "&lt;code&gt;HELLO WORLD&lt;/code&gt;" whereas &lt;code&gt;ret_val1&lt;/code&gt; is "&lt;code&gt;nil&lt;/code&gt;".&lt;br /&gt;&lt;br /&gt;We would expect &lt;code&gt;ret_val1&lt;/code&gt; to also be "&lt;code&gt;HELLO WORLD&lt;/code&gt;". 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't become nil, just the return value of calling that variable is nil. &lt;code&gt;gotcha_string1&lt;/code&gt; will still be "&lt;code&gt;HELLO WORLD&lt;/code&gt;". 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 "!" 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="text/javascript"&gt;&lt;br /&gt;digg_url = 'http://bhanu.blogspot.com/2007/03/gotcha-with-upcase-and-downcase.html';&lt;br /&gt;digg_title = 'Gotcha with Ruby "upcase!" and "downcase!" ';&lt;br /&gt;digg_bodytext = 'In Ruby, upcase! and downcase! don'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.';&lt;br /&gt;digg_topic = 'programming';&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;script src="http://digg.com/tools/diggthis.js" type="text/javascript"&gt;&lt;/script&gt;&lt;br /&gt;&lt;br /&gt;&lt;script src="http://www.google-analytics.com/urchin.js" type="text/javascript"&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;_uacct = "UA-1595086-1";&lt;br /&gt;urchinTracker();&lt;br /&gt;&lt;/script&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4124821-4296308908695765383?l=bhanu.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bhanu.blogspot.com/feeds/4296308908695765383/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4124821&amp;postID=4296308908695765383' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4124821/posts/default/4296308908695765383'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4124821/posts/default/4296308908695765383'/><link rel='alternate' type='text/html' href='http://bhanu.blogspot.com/2007/03/gotcha-with-upcase-and-downcase.html' title='Gotcha with Ruby &quot;upcase!&quot; and &quot;downcase!&quot;'/><author><name>Morampudi Bhanu</name><uri>http://www.blogger.com/profile/03437362908746969144</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4124821.post-6294686557695227253</id><published>2007-03-26T16:00:00.000-07:00</published><updated>2007-03-27T22:49:59.072-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming Tips'/><title type='text'>Capitalizing The First Letter Of Each Word In A String</title><content type='html'>Say we have a string "This is my String" - we want to capitalize the first letter of each word so that my string becomes "This Is My String". 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 = "This is a String" &lt;/code&gt;&lt;br /&gt;&lt;code&gt; my_string.capitalize --&gt; "This is a string" &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 "T" 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 = "this is a String", &lt;/code&gt;&lt;br /&gt;&lt;code&gt; my_string1.capitalize --&gt; "This is a string" &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Notice that "t" in "this" became "T" and "S" in "string" became "s".&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; "This Is A String" &lt;/code&gt;&lt;br /&gt;&lt;br /&gt;We used &lt;code&gt; "capitalize!" &lt;/code&gt; instead of &lt;code&gt; "capitalize" &lt;/code&gt;. The difference is that &lt;b&gt; &lt;code&gt; "capitalize!" &lt;/code&gt; &lt;/b&gt; capitalizes the word in place whereas  &lt;b&gt; &lt;code&gt; "capitalize" &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; "THIS IS A STRING"&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4124821-6294686557695227253?l=bhanu.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bhanu.blogspot.com/feeds/6294686557695227253/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4124821&amp;postID=6294686557695227253' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4124821/posts/default/6294686557695227253'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4124821/posts/default/6294686557695227253'/><link rel='alternate' type='text/html' href='http://bhanu.blogspot.com/2007/03/capitalizing-first-letter-of-each-word.html' title='Capitalizing The First Letter Of Each Word In A String'/><author><name>Morampudi Bhanu</name><uri>http://www.blogger.com/profile/03437362908746969144</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4124821.post-3141303781768181743</id><published>2007-03-26T15:40:00.000-07:00</published><updated>2007-03-26T15:59:59.912-07:00</updated><title type='text'>First Post</title><content type='html'>First post...getting started. I will try to post about Ruby, Rails and JRuby as and when I find the time.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4124821-3141303781768181743?l=bhanu.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bhanu.blogspot.com/feeds/3141303781768181743/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4124821&amp;postID=3141303781768181743' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4124821/posts/default/3141303781768181743'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4124821/posts/default/3141303781768181743'/><link rel='alternate' type='text/html' href='http://bhanu.blogspot.com/2007/03/first-post.html' title='First Post'/><author><name>Morampudi Bhanu</name><uri>http://www.blogger.com/profile/03437362908746969144</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
