"args = nil" in method signature

Hello, please could somebody explain to me what the “args = nil” in the
method signature does? And why/when would you use such a convention?

def request_post(resource, args = nil)
request(resource, “post”, args)
end

example method call on request_post

request_post(“api/v2/projects/my_project/murmur.xml”,
‘murmur[body]’ => “I murmur this message here”)

Thank-you in advance, Michelle

When the signature of the method is variable=some_value, means that
whenthe method is called without passing that parameter will
automatically be assigned the value given to it.
In your case, args will be automatically nil.

  • Adão Raul

Michelle P. wrote in post #1018189:

Hello, please could somebody explain to me what the “args = nil” in the
method signature does? And why/when would you use such a convention?

def my_meth(args=nil)
if args
args.each do |k, v|
puts “#{k}: #{v}”
end
else
puts “goodbye”
end
end

my_meth(a: “hello”, b: “world”)
my_meth()

–output:–
a: hello
b: world
goodbye

great. got it! thanks Adão and 7stud =)