Optional parameters: Difference between revisions

Content added Content deleted
(→‎{{header|Ruby}}: Fill in sort code, showing how to access the hash. Add another example for Ruby 2.0, using MRI 2.0.0dev (2012-01-04 trunk 34211).)
Line 1,213: Line 1,213:
=={{header|Ruby}}==
=={{header|Ruby}}==
Ruby does provide a mechanism to specify default values for method arguments:
Ruby does provide a mechanism to specify default values for method arguments:
<lang ruby>def tablesort(table, ordering=:sort_proc, column=0, reverse=false)
<lang ruby>def tablesort(table, ordering=proc {|a, b| a <=> b}, column=0, reverse=false)
# ...</lang>
# ...</lang>
However, you cannot pass named parameters: if you want to pass "reverse=true", you must also give values for ordering and column.
However, you cannot pass named parameters: if you want to pass "reverse=true", you must also give values for ordering and column.


The idiomatic way in Ruby is to pass a hash or name=>value pairs as method arguments, like this:
The idiomatic way in Ruby is to pass a hash or name=>value pairs as method arguments, like this:
<lang ruby>def tablesort(table, options={})
<lang ruby>def table_sort(table, options = {})
# provide default values then merge in user's options
# Provide default values, then merge in caller's options.
options = {
opts = {"ordering" => :sort_proc, "column" => 0, "reverse" => false}.merge(options)
:ordering => proc {|a, b| a <=> b}, :column => 0, :reverse => false
}.merge(options)


# ... rest of code, for example:
# Rest of code:
column = options[:column]
opts.each_pair {|name, value| puts "#{name} => #{value}"}
p = options[:ordering].to_proc
if options[:reverse]
table.sort {|a, b| p.call(b[column], a[column])}
else
table.sort {|a, b| p.call(a[column], b[column])}
end
end
end


# Quick example:
tablesort(data, "reverse" => true, "column" => 3)</lang>
table = [
["Ottowa", "Canada"],
["Washington", "USA"],
["Mexico City", "Mexico"],
]
p table_sort(table, :column => 1)</lang>

Unknown arguments get ignored. For example, <code>table_sort(table, :typo => 42)</code> just ignores the unknown ''typo'' parameter.

Starting with Ruby 1.9, the shortcut syntax <code>column: 1</code> is same as <code>:column => 1</code>.

{{works with|Ruby|1.9}}
<lang ruby>def table_sort(table, options = {})
# Provide default values, then merge in caller's options.
options = {
ordering: proc {|a, b| a <=> b}, column: 0, reverse: false
}.merge(options)

# ...
end
# ...
p table_sort(table, column: 1)</lang>

===Ruby's future===
A future version, Ruby 2.0 will have keyword arguments.

{{works with|MRI|2.0.0dev (2012-01-04 trunk 34211)}}

<lang ruby>def table_sort(table, ordering: :<=>, column: 0, reverse: false)
p = ordering.to_proc
if reverse
table.sort {|a, b| p.(b[column], a[column])}
else
table.sort {|a, b| p.(a[column], b[column])}
end
end</lang>

Now ''ordering'', ''column'' and ''reverse'' are local variables, not hash keys. Further, this feature also checks for invalid keys. For example, <code>table_sort(table, typo: 42)</code> raises ArgumentError.


=={{header|Scala}}==
=={{header|Scala}}==