<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Appending to a string</title>
	<atom:link href="http://www.rubyfleebie.com/appending-to-a-string/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rubyfleebie.com/appending-to-a-string/</link>
	<description>Because programming should be fun</description>
	<lastBuildDate>Wed, 04 Jan 2012 05:17:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Gib</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-1442</link>
		<dc:creator>Gib</dc:creator>
		<pubDate>Thu, 28 Jul 2011 08:12:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-1442</guid>
		<description>tim, I found this page with a google search because I was trying to figure out why a string of mine (&quot;base&quot; in your example) was changing, apparently in parallel with the contents of another string (&quot;a&quot; in your example).

I was doing exactly what you describe there. Thanks for helping me understand what was going on.
I changed it to
base = &#039;base string&#039;
a = &#039;&#039;;
a &lt;&lt; base;
a &lt;&lt; &quot; some additional stuff &quot;
a &lt;&lt; &quot; more additional stuff &quot;

which fixed my problem.</description>
		<content:encoded><![CDATA[<p>tim, I found this page with a google search because I was trying to figure out why a string of mine (&#8220;base&#8221; in your example) was changing, apparently in parallel with the contents of another string (&#8220;a&#8221; in your example).</p>
<p>I was doing exactly what you describe there. Thanks for helping me understand what was going on.<br />
I changed it to<br />
base = &#8216;base string&#8217;<br />
a = &#8221;;<br />
a &lt;&lt; base;<br />
a &lt;&lt; &quot; some additional stuff &quot;<br />
a &lt;&lt; &quot; more additional stuff &quot;</p>
<p>which fixed my problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tim</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-1382</link>
		<dc:creator>tim</dc:creator>
		<pubDate>Wed, 16 Feb 2011 19:21:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-1382</guid>
		<description>In general &lt;&lt; can have unintended side effects and should be avoided.  += will only modify the exact variable that you are working with.

Say you&#039;re building up a string from a base which you want to remain the same.
base = &#039;base string&#039;
a = base
a &lt;&lt; &quot; some additional stuff &quot;
a &lt;&lt; &quot; more additional stuff &quot;

Unfortunately, you&#039;ve also modified the value of base at this point.</description>
		<content:encoded><![CDATA[<p>In general &lt;&lt; can have unintended side effects and should be avoided.  += will only modify the exact variable that you are working with.</p>
<p>Say you&#039;re building up a string from a base which you want to remain the same.<br />
base = &#039;base string&#039;<br />
a = base<br />
a &lt;&lt; &quot; some additional stuff &quot;<br />
a &lt;&lt; &quot; more additional stuff &quot;</p>
<p>Unfortunately, you&#039;ve also modified the value of base at this point.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RSL</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-247</link>
		<dc:creator>RSL</dc:creator>
		<pubDate>Wed, 13 Jun 2007 13:05:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-247</guid>
		<description>And it didn&#039;t. :(</description>
		<content:encoded><![CDATA[<p>And it didn&#8217;t. :(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RSL</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-246</link>
		<dc:creator>RSL</dc:creator>
		<pubDate>Wed, 13 Jun 2007 13:04:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-246</guid>
		<description>If all you&#039;re trying to do is print a concatenated string, though. I think I remember [from benchmarking] that puts &quot;#{foo} #{bar}&quot; 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: Arne</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-80</link>
		<dc:creator>Arne</dc:creator>
		<pubDate>Thu, 05 Apr 2007 10:57:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-80</guid>
		<description>Supose you write a method that returns its argument with &#039;+++&#039; appended to it :

def m1(s); s + &quot;+++&quot;;end
def m2(s); s &lt;&lt; &quot;+++&quot;; end

a=&#039;abc&#039;             #=&gt; &quot;abc&quot;

m1(a)              #=&gt; &quot;abc+++&quot;
a                     #=&gt; &quot;abc&quot;

m2(a)              #=&gt; &quot;abc+++&quot;
a                     #=&gt; &quot;abc+++&quot;

The point is that with &lt;&lt; you&#039;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&#039;m overgeneralizing ofcourse).

This is the reason that strings in Java (eww.. don&#039;t mention the J-word...) are immutable. There&#039;s a StringBuffer for when mutable strings are needed.

But if you know what you&#039;re doing, why not. The nice thing is that your algorithm might work unchanged with arrays and possible other objects due to ruby&#039;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: Frank</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-79</link>
		<dc:creator>Frank</dc:creator>
		<pubDate>Thu, 05 Apr 2007 10:24:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-79</guid>
		<description>Phew! I had a hard time to understand this... It&#039;s true that my comment system is bad at keeping formatting. I&#039;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&#039;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: Anselm</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-78</link>
		<dc:creator>Anselm</dc:creator>
		<pubDate>Thu, 05 Apr 2007 10:13:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-78</guid>
		<description>The point of Gregory&#039;s comment is that 

a += b

generates a new object, while 

a &lt;&lt; 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: Todd Werth</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-73</link>
		<dc:creator>Todd Werth</dc:creator>
		<pubDate>Wed, 04 Apr 2007 22:58:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-73</guid>
		<description>It&#039;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: Gregory</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-72</link>
		<dc:creator>Gregory</dc:creator>
		<pubDate>Wed, 04 Apr 2007 22:45:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#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. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gregory</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-71</link>
		<dc:creator>Gregory</dc:creator>
		<pubDate>Wed, 04 Apr 2007 22:44:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-71</guid>
		<description>Hmm... the above seems to lost formatting. :-/

I said it&#039;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>
</channel>
</rss>

