Skip to content

Commit

Permalink
[Docs] Improve example of Lint/ToJSON (#8605)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredbeck committed Sep 15, 2020
1 parent 3282218 commit 0d3f5e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
21 changes: 16 additions & 5 deletions docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -3833,12 +3833,23 @@ for an optional argument, your method should too.

[source,ruby]
----
# bad
def to_json
end
class Point
attr_reader :x, :y
# good
def to_json(*_args)
# bad, incorrect arity
def to_json
JSON.generate([x, y])
end
# good, preserving args
def to_json(*args)
JSON.generate([x, y], *args)
end
# good, discarding args
def to_json(*_args)
JSON.generate([x, y])
end
end
----

Expand Down
21 changes: 16 additions & 5 deletions lib/rubocop/cop/lint/to_json.rb
Expand Up @@ -9,12 +9,23 @@ module Lint
# for an optional argument, your method should too.
#
# @example
# # bad
# def to_json
# end
# class Point
# attr_reader :x, :y
#
# # bad, incorrect arity
# def to_json
# JSON.generate([x, y])
# end
#
# # good, preserving args
# def to_json(*args)
# JSON.generate([x, y], *args)
# end
#
# # good
# def to_json(*_args)
# # good, discarding args
# def to_json(*_args)
# JSON.generate([x, y])
# end
# end
#
class ToJSON < Base
Expand Down

0 comments on commit 0d3f5e2

Please sign in to comment.