<?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>Global Nerdy &#187; named parameters. method calls</title>
	<atom:link href="http://www.globalnerdy.com/tag/named-parameters-method-calls/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.globalnerdy.com</link>
	<description>Tech Evangelist Joey deVilla on Shopify, startups, software development, tech news and other nerdy stuff</description>
	<lastBuildDate>Thu, 09 Feb 2012 18:30:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Named Parameters in Method Calls: Python Si, Ruby No</title>
		<link>http://www.globalnerdy.com/2009/03/16/named-parameters-in-method-calls-python-si-ruby-no/</link>
		<comments>http://www.globalnerdy.com/2009/03/16/named-parameters-in-method-calls-python-si-ruby-no/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 04:00:00 +0000</pubDate>
		<dc:creator>Joey deVilla</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[named arguments]]></category>
		<category><![CDATA[named parameters. method calls]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.globalnerdy.com/2009/03/16/named-parameters-in-method-calls-python-si-ruby-no/</guid>
		<description><![CDATA[In an earlier article, Default and Named Parameters in C# 4.0 / Sith Lord in Training, I wrote about how C# 4.0 – that’s the version coming out with the next release of Visual Studio, known as Visual Studio 2010 – is going to provide support for named parameters. In that article, I also incorrectly [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><img style="border-right-width: 0px; margin: 0px 0px 5px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="&quot;Hello My Name Is&quot; sticker" border="0" alt="&quot;Hello My Name Is&quot; sticker" align="right" src="http://www.globalnerdy.com/wordpress/wp-content/uploads/2009/03/hello-my-name-is-sticker.jpg" width="241" height="206" /> In an earlier article, <em><strong><a href="http://www.globalnerdy.com/2009/03/12/default-and-named-parameters-in-c-40-sith-lord-in-training/">Default and Named Parameters in C# 4.0 / Sith Lord in Training</a></strong></em>, I wrote about how C# 4.0 – that’s the version coming out with the next release of Visual Studio, known as <a href="http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx">Visual Studio 2010</a> – is going to provide support for named parameters.</p>
<p>In that article, I also <em>incorrectly </em>stated that Ruby supported named parameters. Luckily, <a href="http://www.globalnerdy.com/2009/03/12/default-and-named-parameters-in-c-40-sith-lord-in-training/#comment-3982">Jörg W Mittag spotted my mistake an corrected me in a comment</a>. I’ve since corrected the article and thought I’d show you how I got it wrong in the first place.</p>
<h3>Ruby and My Named Parameter Goof</h3>
<p>I had a vague recollection of Ruby accepting named parameters. I figured I’d be empirical and fired up <strong>irb</strong> – the Ruby REPL shell – and put together a quick little method to see if the recollection was correct:</p>
<blockquote><pre># Ruby 1.8.6
def test_named_params(first, second)
    puts &quot;#{first}\n#{second}&quot;
end</pre>
</blockquote>
<p>Once put together, I made some test calls to the method:</p>
<blockquote>
<pre>

# irb session (Ruby 1.8.6)
irb(main):&gt; <strong>test_named_params(&quot;alpha&quot;, &quot;beta&quot;)</strong>
alpha
beta

=&gt; nil

irb(main):&gt; <strong>test_named_params(first = &quot;alpha&quot;, second = &quot;beta&quot;)</strong>
alpha
beta

=&gt; nil
</pre>
</blockquote>
<p>Seeing that the interpreter didn’t choke on that named parameter call, I thought to myself “Vague recollection confirmed, Ruby supports named parameters!” and wrote the blog article.</p>
<p>Had my brain actually been firing on all cylinders, I would’ve given the method a proper test by providing the named parameters out of the order in which they appear in the method signature. Here’s what I would’ve seen:</p>
<blockquote>
<pre># irb session (Ruby 1.8.6)
irb(main):&gt; <strong>test_named_params(second = &quot;alpha&quot;, first = &quot;beta&quot;)</strong>
alpha
beta


=&gt; nil

</pre>
</blockquote>
<p>Uh-oh. If named parameters worked, the first output line would be “beta” and the second would be “alpha”. Clearly something’s wrong with my recollection.</p>
<p>Let’s try some <em>non-existent</em> named parameters – say, ones involving current entertainemtn news headlines &#8212; just to see what happens:</p>
<blockquote>
<pre># irb session (Ruby 1.8.6)
irb(main):&gt; <strong>test_named_params(lindsay_lohan_dui = &quot;alpha&quot;,
jim_cramer_smackdown = &quot;beta&quot;)</strong>


alpha

beta

=&gt; nil
</pre>
</blockquote>
<p>Even with nonsensical named parameters, the method is still accepting the values in order. Why is that?</p>
<p>Just about everything in Ruby has a return value (which can be anything, including <code>nil</code>). You can see for yourself in irb – here’s a quick do-nothing method definition:</p>
<blockquote>
<pre>irb(main)&gt; <strong>def doNothing
</strong>irb(main)&gt; <strong>end</strong>
=&gt; nil</pre>
</blockquote>
<p>As you can see. defining a method returns a value of <code>nil</code>. </p>
<p>As Jorg pointed out, Ruby assignment statements return a value: the value used in the assigment. Once again, for proof, I&#8217;ll use an example from an irb session. In the example below, assigning the string <code>&quot;alpha&quot;</code> to the variable <code>first</code> also returns the string <code>&quot;alpha&quot;</code>:</p>
<blockquote>
<pre># irb session (Ruby 1.8.6)
irb(main):&gt; <strong>first = &quot;alpha&quot;</strong>=&gt; &quot;alpha&quot;</pre>
</blockquote>
<p>In the call to <code>test_named_params</code>, the Ruby interpreter was interpreting my “named parameters” as assignment statements. <code>first = &quot;alpha&quot;</code> evaluates to plain old <code>&quot;alpha&quot;</code>, but so does <code>second = &quot;alpha&quot;</code> (and for that matter, so does <code>lindsay_lohan_dui = &quot;alpha&quot;</code>). Each assignment statement in my parameter list was evaluated, and then those values were passed to method in positional order.</p>
<h3>Python Supports Named Parameters</h3>
<p>After getting the comment from Jorg and correcting my article, I wondered why I thought Ruby supported named parameters. Then it hit me – it’s Python.</p>
<p>So I fired up the Python REPL and put together this quick little method:</p>
<blockquote>
<pre># Python 3.0
def test_named_params(first, second):
    print(&quot;%s\n%s&quot; % (first, second))</pre>
</blockquote>
<p>And this time, I decided to be a little more thorough in my testing:</p>
<blockquote>
<pre># Python 3.0 REPL
&gt;&gt;&gt; <strong>test_named_params(&quot;alpha&quot;, &quot;beta&quot;)</strong>
alpha
beta

&gt;&gt;&gt; <strong>test_named_params(first = &quot;alpha&quot;, second = &quot;beta&quot;)</strong>
alpha
beta

&gt;&gt;&gt; <strong>test_named_params(second = &quot;alpha&quot;, first = &quot;beta&quot;)</strong>
beta
alpha</pre>
</blockquote>
<p>And some additional searching on the web confirmed that yes, Python method calling does in fact support named parameters.</p>
<p>So in conclusion, when it comes to named parameters, it’s <em>Python si, Ruby no</em>…and <em>C# <a href="http://dictionary.reverso.net/english-spanish/soon">pronto</a></em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.globalnerdy.com/2009/03/16/named-parameters-in-method-calls-python-si-ruby-no/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

