<?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>Thu, 15 Jul 2010 19:48:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<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>
	<item>
		<title>By: Gregory</title>
		<link>http://www.rubyfleebie.com/appending-to-a-string/comment-page-1/#comment-70</link>
		<dc:creator>Gregory</dc:creator>
		<pubDate>Wed, 04 Apr 2007 22:40:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubyfleebie.com/use/#comment-70</guid>
		<description>String#
&gt;&gt; a = &quot;foo&quot;
=&gt; &quot;foo&quot;
&gt;&gt; a  &quot;foobar&quot;
&gt;&gt; a = &quot;foo&quot;
=&gt; &quot;foo&quot;
&gt;&gt; a.object_id
=&gt; 1686724
&gt;&gt; a  &quot;foobar&quot;
&gt;&gt; a.object_id
=&gt; 1686724
&gt;&gt; b = &quot;foo&quot;
=&gt; &quot;foo&quot;
&gt;&gt; b.object_id
=&gt; 1668094
&gt;&gt; b += &quot;bar&quot;
=&gt; &quot;foobar&quot;
&gt;&gt; b.object_id
=&gt; 1657194
&gt;&gt; a == b
=&gt; 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>
</channel>
</rss>
