Problem in argument to the function

def hi(a,b=nil)
puts a
puts b
end

hi 1,2

It allows me to assign the default value to the second argument of the
function as shown above,but when I tend to assign default value to the
first argument it throws the error as show below,
def hi(a=nil,b)
puts a
puts b
end

hi 1,2

Error
hi.rb:1: syntax error, unexpected ‘)’, expecting ‘=’
hi.rb:5: syntax error, unexpected kEND, expecting $end

Could any one suggest me what’s the problem here?

Hello,

On 18 Φεβ 2014, at 11:05 , Raja gopalan [email protected] wrote:

def hi(a=nil,b)
puts a
puts b

end

hi 1,2

Works fine here:

┌─[atma@air] - [~] - [Τρι Φεβ 18, 03:43]
└─[$] <> cat test.rb && ruby test.rb
def hi(a=nil,b)
puts a
puts b

end

hi 1,2
1
2

┌─[atma@air] - [~] - [Τρι Φεβ 18, 03:43]
└─[$] <> ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

On Tue, Feb 18, 2014 at 5:05 AM, Raja gopalan [email protected]
wrote:

It allows me to the default value to the second argument of the
function as shown above,but when I tend to assign default
value to the first argument it throws the error as show below,

That’s because you can’t have any required parameters after any
optional ones. Think about how the parser would know which one you’re
trying to assign a value to.

Accepting a hash, with some default values inside your method, might
be a better option.

-Dave