Michael Morin wrote:
> On Mon, Jun 29, 2009 at 8:20 AM, Ibon Castilla<ibon.castilla / gmail.com> 
> wrote:
>> my_method('Ruby')
>> --
>> Posted via http://www.ruby-forum.com/.
> 
> Optional arguments must be passed in the order they're declared, and
> cannot be omitted.  Instead, you'll often see hashes used instead.
> 
> def my_method(options={})
>   o = {
>     :a => 'This',
>     :b => 'is',
>     :c => 'cool'
>   }.merge(options)
> 
>   puts "#{o[:a]} #{o[:b]} #{o[:c]}"
> end
> 
> my_method( :c => 'great' )

I agree that's probably the best approach in most cases. If you really 
want 'optional' earlier arguments, you'd have to do something like this:

def my_method(a=nil, b=nil, c=nil)
  a ||= 'This'
  b ||= 'is'
  c ||= 'great'
  puts a+' '+b+' '+c
end

p my_method(nil, nil, 'cool')
-- 
Posted via http://www.ruby-forum.com/.