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
Written by David Underwood
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#