<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Khaled alHabache's official blog &#187; pass-by-value</title>
	<atom:link href="http://www.khelll.com/blog/tag/pass-by-value/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.khelll.com/blog</link>
	<description>What web development means....</description>
	<lastBuildDate>Tue, 16 Mar 2010 23:21:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby, pass by value or by reference?</title>
		<link>http://www.khelll.com/blog/ruby/ruby-pass-by-value-or-by-reference/</link>
		<comments>http://www.khelll.com/blog/ruby/ruby-pass-by-value-or-by-reference/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 09:49:57 +0000</pubDate>
		<dc:creator>khelll</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[pass-by-refrence]]></category>
		<category><![CDATA[pass-by-value]]></category>

		<guid isPermaLink="false">http://www.khelll.com/blog/?p=303</guid>
		<description><![CDATA[It&#8217;s a basic question that I myself had a problem with when i started using Ruby: Does Ruby pass by value or by reference?
Well, if you want a direct answer, then: Ruby passes by value. It&#8217;s a similar behavior of what Java does.
Let&#8217;s prove it via examples:
Update:  this example was passing integers, and it [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a basic question that I myself had a problem with when i started using Ruby: Does Ruby pass by value or by reference?<br />
Well, if you want a direct answer, then: Ruby passes by value. It&#8217;s a similar behavior of what Java does.</p>
<p>Let&#8217;s prove it via examples:<br />
<strong>Update:</strong>  this example was passing integers, and it turned out that even everything in ruby is an object, but immedaite values are passed directly by their values, so i changed this example to let it pass strings instead.</p>
<p>Let&#8217;s pass an argument to a method and check what goes on:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:003:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#9966CC; font-weight:bold;">def</span> change<span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>; x = <span style="color:#996600;">'10'</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:004:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> y = <span style="color:#996600;">'3'</span>; <span style="color:#CC0066; font-weight:bold;">puts</span> change<span style="color:#006600; font-weight:bold;">&#40;</span>y<span style="color:#006600; font-weight:bold;">&#41;</span> ,  y
<span style="color:#996600;">'10'</span>
<span style="color:#996600;">'3'</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span></pre></div></div>

<p>As you see, we defined a method &#8216;change&#8217; that takes a parameter &#8216;x&#8217;, changes it value and then returns it back. Then we invoked that method on the argument &#8216;y&#8217; which had the value &#8216;3&#8242;, and kept having the same value after the method invocation.<br />
This simple example proves that the argument is not passed by reference, otherwise the value of &#8216;y&#8217; would rather changed to &#8216;10&#8242; after the method invocation.</p>
<p>Now let&#8217;s take another example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:005:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#9966CC; font-weight:bold;">def</span> another_change<span style="color:#006600; font-weight:bold;">&#40;</span>x<span style="color:#006600; font-weight:bold;">&#41;</span>; x<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">'f'</span>; x; <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span>
irb<span style="color:#006600; font-weight:bold;">&#40;</span>main<span style="color:#006600; font-weight:bold;">&#41;</span>:006:<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&gt;</span> s = <span style="color:#996600;">&quot;hello&quot;</span>; <span style="color:#CC0066; font-weight:bold;">puts</span> another_change<span style="color:#006600; font-weight:bold;">&#40;</span>s<span style="color:#006600; font-weight:bold;">&#41;</span> , s
fello
fello
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">nil</span></pre></div></div>

<p>In this example, the passed argument value &#8216;hello&#8217; has changed to &#8216;fello&#8217;, and thus it proves that the argument is not passed by value, otherwise the value of &#8217;s&#8217; would kept having the same value &#8216;hello&#8217;.</p>
<p>Thus it&#8217;s not by value, neither by reference, then what is it? </p>
<p>As you know, everything in Ruby is an object, and thus, doing:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">value = <span style="color:#996600;">&quot;smthin&quot;</span></pre></div></div>

<p>means: assign a <strong>reference</strong>(a place in memory that points to referenced value. In other words, it&#8217;s a place that holds the memory address of the referenced value) of the string &#8217;somthin&#8217; to the variable named &#8216;val&#8217;, and normally when working with the variable &#8216;val&#8217;, it means we are working on it&#8217;s referenced object &#8217;smthing&#8217;. </p>
<p>But when invoking a method:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Foo<span style="color:#006600; font-weight:bold;">&#40;</span>val<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Things change a bit, as Ruby passes <strong>a copy</strong> of the &#8216;val&#8217; <strong>reference itself</strong>(which is the memory address of the referenced object), not it&#8217;s referenced object, to the method Foo, and that copy will still reference the same object &#8217;smthing&#8217; as the variable &#8216;val&#8217; does.<br />
This is called: Pass by Value, <strong>a copy of a reference</strong> to an object is passed, and so, not the referenced object itself is passed, neither the argument itself.<br />
Then Ruby passes by value. Some ppl like to call it &#8220;pass by reference value&#8221;, but that&#8217;s not standard afaik.</p>
<p>That explains what goes in both examples, as in the first example the value of the argument &#8216;y&#8217; didn&#8217;t change due to the fact that we are not passing the reference &#8216;y&#8217; itself, instead a copy of it, and thus, when its copy referenced another object(via assignment operator), the original variable &#8216;y&#8217; kept referencing it&#8217;s object, the object with value &#8216;3&#8242; in our case. As for the second example, the argument &#8217;s&#8217; changed due to the fact that its passed copy is still referencing the same object &#8216;hello&#8217;, but the operator [] changed the referenced object value, and thus got a new value &#8216;fello&#8217;.</p>
<p>I hope the idea is clear now, if not, please don&#8217;t hesitate to comment.</p>
<p><strong>Update:</strong><br />
I have blogged a <a href="http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/">new article</a> explaining passing by reference and value in 3 languages c++, java and ruby </p>
]]></content:encoded>
			<wfw:commentRss>http://www.khelll.com/blog/ruby/ruby-pass-by-value-or-by-reference/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
	</channel>
</rss>
