Skip to content

Commit

Permalink
Remove unnecessary code
Browse files Browse the repository at this point in the history
In `JSON#generate` and `JSON#fast_generate`:

- When the given `opts` is a `JSON::State` the variable is set to
  `nil`.
- But it will be never used as the next `if` blocks will not be
  executed.
- `JSON::State#configure` does the conversion to `Hash`, the
  conversions in the `if` block are just duplication.
- `JSON::State,new` does the same thing with `configure` when an
  argument is given.
  • Loading branch information
nobu committed Jul 17, 2023
1 parent b066be0 commit 3825179
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,19 +295,9 @@ def load_file!(filespec, opts = {})
#
def generate(obj, opts = nil)
if State === opts
state, opts = opts, nil
state = opts
else
state = State.new
end
if opts
if opts.respond_to? :to_hash
opts = opts.to_hash
elsif opts.respond_to? :to_h
opts = opts.to_h
else
raise TypeError, "can't convert #{opts.class} into Hash"
end
state = state.configure(opts)
state = State.new(opts)
end
state.generate(obj)
end
Expand All @@ -334,19 +324,9 @@ def generate(obj, opts = nil)
# JSON.fast_generate(a)
def fast_generate(obj, opts = nil)
if State === opts
state, opts = opts, nil
state = opts
else
state = JSON.create_fast_state
end
if opts
if opts.respond_to? :to_hash
opts = opts.to_hash
elsif opts.respond_to? :to_h
opts = opts.to_h
else
raise TypeError, "can't convert #{opts.class} into Hash"
end
state.configure(opts)
state = JSON.create_fast_state.configure(opts)
end
state.generate(obj)
end
Expand Down

0 comments on commit 3825179

Please sign in to comment.