Last Updated: February 25, 2016
·
1.183K
· davefp

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