3j_zva
Last Updated: February 25, 2016
·
529
· davefp
750dd4aa3b45974dc093ec8fe4b4ee68

Don't forget to accept args when writing to_json methods

When writing custom to_json methods on your ruby objects, don't forget that you need to accept arguments.

The following will fail with a wrong number of arguments (1 for 0) (ArgumentError) error:

def to_json
  {'name' => name}.to_json
end

Here's the correct version:

def to_json(*args)
  {'name' => name}.to_json(*args)
end
Say Thanks
Respond