<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.1.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Appending to a string</title>
	<link>http://www.rubyfleebie.com/appending-to-a-string/</link>
	<description>Because programming should be fun</description>
	<pubDate>Sat, 22 Nov 2008 08:16:29 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>

	<item>
		<title>By: Gregory</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-70</link>
		<author>Gregory</author>
		<pubDate>Wed, 04 Apr 2007 22:40:59 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-70</guid>
					<description>String#
&#62;&#62; a = "foo"
=&#62; "foo"
&#62;&#62; a  "foobar"
&#62;&#62; a = "foo"
=&#62; "foo"
&#62;&#62; a.object_id
=&#62; 1686724
&#62;&#62; a  "foobar"
&#62;&#62; a.object_id
=&#62; 1686724
&#62;&#62; b = "foo"
=&#62; "foo"
&#62;&#62; b.object_id
=&#62; 1668094
&#62;&#62; b += "bar"
=&#62; "foobar"
&#62;&#62; b.object_id
=&#62; 1657194
&#62;&#62; a == b
=&#62; true


So actually, the two have different behaviors and should be used where appropriate, rather than preferring one over the other.

Use String#= b  is equivalent to a = a  b</description>
		<content:encoded><![CDATA[<p>String#<br />
&gt;&gt; a = &#8220;foo&#8221;<br />
=&gt; &#8220;foo&#8221;<br />
&gt;&gt; a  &#8220;foobar&#8221;<br />
&gt;&gt; a = &#8220;foo&#8221;<br />
=&gt; &#8220;foo&#8221;<br />
&gt;&gt; a.object_id<br />
=&gt; 1686724<br />
&gt;&gt; a  &#8220;foobar&#8221;<br />
&gt;&gt; a.object_id<br />
=&gt; 1686724<br />
&gt;&gt; b = &#8220;foo&#8221;<br />
=&gt; &#8220;foo&#8221;<br />
&gt;&gt; b.object_id<br />
=&gt; 1668094<br />
&gt;&gt; b += &#8220;bar&#8221;<br />
=&gt; &#8220;foobar&#8221;<br />
&gt;&gt; b.object_id<br />
=&gt; 1657194<br />
&gt;&gt; a == b<br />
=&gt; true</p>
<p>So actually, the two have different behaviors and should be used where appropriate, rather than preferring one over the other.</p>
<p>Use String#= b  is equivalent to a = a  b</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Gregory</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-71</link>
		<author>Gregory</author>
		<pubDate>Wed, 04 Apr 2007 22:44:28 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-71</guid>
					<description>Hmm... the above seems to lost formatting. :-/

I said it's not precedence but the fact that String#+ and String#</description>
		<content:encoded><![CDATA[<p>Hmm&#8230; the above seems to lost formatting. :-/</p>
<p>I said it&#8217;s not precedence but the fact that String#+ and String#</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Gregory</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-72</link>
		<author>Gregory</author>
		<pubDate>Wed, 04 Apr 2007 22:45:29 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-72</guid>
					<description>Okay, your comment system sucks... hope you can reformat the above into a single comment. :)</description>
		<content:encoded><![CDATA[<p>Okay, your comment system sucks&#8230; hope you can reformat the above into a single comment. <img src='http://www.rubyfleebie.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Todd Werth</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-73</link>
		<author>Todd Werth</author>
		<pubDate>Wed, 04 Apr 2007 22:58:36 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-73</guid>
					<description>It's very true that you should use </description>
		<content:encoded><![CDATA[<p>It&#8217;s very true that you should use</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Anselm</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-78</link>
		<author>Anselm</author>
		<pubDate>Thu, 05 Apr 2007 10:13:16 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-78</guid>
					<description>The point of Gregory's comment is that 

a += b

generates a new object, while 

a &#60;&#60; b

appends b to the existing object a.</description>
		<content:encoded><![CDATA[<p>The point of Gregory&#8217;s comment is that </p>
<p>a += b</p>
<p>generates a new object, while </p>
<p>a &lt;&lt; b</p>
<p>appends b to the existing object a.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Frank</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-79</link>
		<author>Frank</author>
		<pubDate>Thu, 05 Apr 2007 10:24:29 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-79</guid>
					<description>Phew! I had a hard time to understand this... It's true that my comment system is bad at keeping formatting. I'll try to find some plugin that will fix the problem.

Thanks for the info Gregory, I have updated the original post. However, I can't see a real reason to use += instead of &lt;&lt; when appending to a string...</description>
		<content:encoded><![CDATA[<p>Phew! I had a hard time to understand this&#8230; It&#8217;s true that my comment system is bad at keeping formatting. I&#8217;ll try to find some plugin that will fix the problem.</p>
<p>Thanks for the info Gregory, I have updated the original post. However, I can&#8217;t see a real reason to use += instead of << when appending to a string&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: Arne</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-80</link>
		<author>Arne</author>
		<pubDate>Thu, 05 Apr 2007 10:57:22 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-80</guid>
					<description>Supose you write a method that returns its argument with '+++' appended to it :

def m1(s); s + "+++";end
def m2(s); s &#60;&#60; "+++"; end

a='abc'             #=&#62; "abc"

m1(a)              #=&#62; "abc+++"
a                     #=&#62; "abc"

m2(a)              #=&#62; "abc+++"
a                     #=&#62; "abc+++"

The point is that with &#60;&#60; you're modifying the instance, but maybe somewhere else there exists a reference to the same instance where the string is not expected to change. In general it can be good practice not to modify the objects passed into a method. (I'm overgeneralizing ofcourse).

This is the reason that strings in Java (eww.. don't mention the J-word...) are immutable. There's a StringBuffer for when mutable strings are needed.

But if you know what you're doing, why not. The nice thing is that your algorithm might work unchanged with arrays and possible other objects due to ruby's lovely duck-typing philosophy.</description>
		<content:encoded><![CDATA[<p>Supose you write a method that returns its argument with &#8216;+++&#8217; appended to it :</p>
<p>def m1(s); s + &#8220;+++&#8221;;end<br />
def m2(s); s &lt;&lt; &#8220;+++&#8221;; end</p>
<p>a=&#8217;abc&#8217;             #=&gt; &#8220;abc&#8221;</p>
<p>m1(a)              #=&gt; &#8220;abc+++&#8221;<br />
a                     #=&gt; &#8220;abc&#8221;</p>
<p>m2(a)              #=&gt; &#8220;abc+++&#8221;<br />
a                     #=&gt; &#8220;abc+++&#8221;</p>
<p>The point is that with &lt;&lt; you&#8217;re modifying the instance, but maybe somewhere else there exists a reference to the same instance where the string is not expected to change. In general it can be good practice not to modify the objects passed into a method. (I&#8217;m overgeneralizing ofcourse).</p>
<p>This is the reason that strings in Java (eww.. don&#8217;t mention the J-word&#8230;) are immutable. There&#8217;s a StringBuffer for when mutable strings are needed.</p>
<p>But if you know what you&#8217;re doing, why not. The nice thing is that your algorithm might work unchanged with arrays and possible other objects due to ruby&#8217;s lovely duck-typing philosophy.</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: RSL</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-246</link>
		<author>RSL</author>
		<pubDate>Wed, 13 Jun 2007 13:04:39 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-246</guid>
					<description>If all you're trying to do is print a concatenated string, though. I think I remember [from benchmarking] that puts "#{foo} #{bar}" is actually a bit faster than puts foo </description>
		<content:encoded><![CDATA[<p>If all you&#8217;re trying to do is print a concatenated string, though. I think I remember [from benchmarking] that puts &#8220;#{foo} #{bar}&#8221; is actually a bit faster than puts foo</p>
]]></content:encoded>
				</item>
	<item>
		<title>By: RSL</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/#comment-247</link>
		<author>RSL</author>
		<pubDate>Wed, 13 Jun 2007 13:05:16 +0000</pubDate>
		<guid>http://www.rubyfleebie.com/appending-to-a-string/#comment-247</guid>
					<description>And it didn't. :(</description>
		<content:encoded><![CDATA[<p>And it didn&#8217;t. <img src='http://www.rubyfleebie.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /></p>
]]></content:encoded>
				</item>
</channel>
</rss>
