Skip to content

Commit 29dad14

Browse files
koicbyroot
authored andcommitted
Fix JSON.load_file and JSON.load_file! to load on Ruby 2.7.0 through 2.7.2
Since ebf0f29, `load_file` and `load_file!` forward their arguments to `parse` and `parse!` with `...` after a leading parameter. Ruby 2.7 accepts `...` only on its own; a leading parameter before it was added in Ruby 3.0 and reached the 2.7 series in 2.7.3, so on Ruby 2.7.0 through 2.7.2 requiring json 3.0 raises a `SyntaxError` at `lib/json/common.rb`, while the gemspec still allows Ruby 2.7 as a whole. `parse` and `parse!` take keyword arguments only, so forwarding `**options` passes exactly what `...` passed, on every Ruby the gem supports. A `method_missing` helper in the parser tests used the same form; it takes an anonymous splat now, so the test suite itself loads on those versions. Checked with `ruby -c` on Ruby 2.7.0, 2.7.2, 2.7.3, and 3.0.7, and with the test suite on Ruby 2.7.2 and 3.0.7.
1 parent 73edd53 commit 29dad14

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

‎CHANGES.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Unreleased
44

5+
* Fix `JSON.load_file` and `JSON.load_file!` to load on Ruby 2.7.0 through 2.7.2, which cannot parse a leading parameter before `...`.
6+
57
### 2026-09-08 (3.0.1)
68

79
* Restore the `limit` positional argument of `JSON.dump`.

‎lib/json/common.rb‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ def parse!(source, **options)
324324
# parse(File.read(path), **)
325325
#
326326
# See method #parse.
327-
def load_file(filespec, ...)
328-
parse(File.read(filespec, encoding: Encoding::UTF_8), ...)
327+
def load_file(filespec, **options)
328+
parse(File.read(filespec, encoding: Encoding::UTF_8), **options)
329329
end
330330

331331
# :call-seq:
@@ -335,8 +335,8 @@ def load_file(filespec, ...)
335335
# JSON.parse!(File.read(path), **)
336336
#
337337
# See method #parse!
338-
def load_file!(filespec, ...)
339-
parse!(File.read(filespec, encoding: Encoding::UTF_8), ...)
338+
def load_file!(filespec, **options)
339+
parse!(File.read(filespec, encoding: Encoding::UTF_8), **options)
340340
end
341341

342342
# :call-seq:

‎test/json/json_parser_test.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ def []=(k, v)
771771
@attrs[k.to_sym] = v
772772
end
773773

774-
def method_missing(name, ...)
774+
def method_missing(name, *)
775775
@attrs.fetch(name) do
776776
super
777777
end

0 commit comments

Comments
 (0)