Leem.fin
Leem.fin

Reputation: 42582

Unknown argument when call a function in another function

In my module, I defined two functions have the same name but different number of arguments.

module MyMod
 def self.doTask(name:, age:)
    doTask(name: "John", age: 30, career: "Teacher")
 end

 def self.doTask(name:, age:, career:)
   puts "name:#{name}, age:#{age}, career:#{career}"
 end
end

As you see above, in doTask, I just call doTask.

In another Ruby file, I call the doTask by:

MyMod.doTask(name:"Kate", age: 28)

But I get runtime error:

unknown keyword: career (ArgumentError)

Why?

Upvotes: 0

Views: 88

Answers (2)

mikej
mikej

Reputation: 66263

Ruby doesn't support method overloading (2 methods with the same name and different parameters) so your second method definition with the career parameter is replacing the first method definition.

You can provide a default value for the optional parameter like this:

def self.doTask(name:, age:, career: 'Employee')
 puts "name:#{name}, age:#{age}, career:#{career}"
end

and then career will have the value "Employee" if not specified.

or default to nil and include some logic in the method body to handle

def self.doTask(name:, age:, career: nil)
  unless career.nil?
    # doTask with career
  else
    # doTask without career
  end
end

If you're coming to Ruby from another language like Java or C# then there's some great insight into why the equivalent behaviour doesn't exist in Ruby in this answer.

Upvotes: 2

Pascal
Pascal

Reputation: 8637

Ruby does not have method overloading. You can not have multiple methods with the same name.

One solution would be to use a the three argument version of the method and add a default value for the :career argument.

module MyMod
  def self.doTask(name:, age:, career: "Teacher")
    puts "name:#{name}, age:#{age}, career:#{career}"
  end
end
MyMod.doTask(name:"Kate", age: 28)
MyMod.doTask(name:"Kate", age: 28, career: 'Teacher')
MyMod.doTask(name:"Kate", age: 28, career: 'Mechanic')

Upvotes: 4

Related Questions