Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT:</strong> Ruby 2.0 introduces the splat operator and keyword arguments, so if you are using Ruby >= 2.0 you can just use <code>foo(arg, keyword: arg_default)</code> and <code>foo(1, **{:keyword =&gt; arg})</code>. If you are using Ruby 1.x then what follows applies:</p> <hr> <p>Ruby 1.x doesn't have a splat operator for keyword arguments (in fact, Ruby 1.x doesn't even <strong>have</strong> keyword arguments). Instead, you would simply have <code>config</code> as your last argument and users of your function can pass in a hash out of which you pull keys:</p> <pre><code>foo(arg1, arg2, config) bar(newarg1, newarg2, config) </code></pre> <p>In <em>Python</em> you'd define <code>foo</code> and <code>bar</code> in this manner:</p> <pre><code>def foo(arg1, arg2, **config): # config now contains any keyword arguments passed to foo </code></pre> <p>And call it:</p> <pre><code>foo(1, 2, some="key", another="value") # or if you have a hash from elsewhere foo(1, 2, **{"some": "key", "another": "value"}) </code></pre> <p>In <em>Ruby</em> a similar construct is achieved in this manner:</p> <pre><code>def foo(arg1, arg2, config={}) # config is *still* a positional argument even though it has a default value! # do stuff with your configuration hash # as if it were a dictionary of keyword args # for example: default_config = {:options {:key =&gt; 'value'}, :stuff =&gt; 123} config = default_config.merge(config) end </code></pre> <p>And it is called in this manner:</p> <pre><code>foo(1, 2, :some =&gt; 'key', :another =&gt; 'value') # which translates into foo(1, 2, {:some =&gt; 'key', :another =&gt; 'value'}) # the {} are optional for the last argument </code></pre> <p>If we were to translate that Ruby code <em>directly</em> into Python it would look <em>something</em> like this:</p> <pre><code>def foo(arg1, arg2, config=None): # This is not *quite* exact because we can still call `foo(1, 2, config={})` # but I think it is as close as you can get in Python if config is None: config = {} # Now we can do things with the config dictionary. </code></pre> <p>And you would call it:</p> <pre><code># Note: no ** (splat) operator in front of our dictionary foo(1, 2, {"some": "key", "another": "value"}) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload