Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Kernel.fail is able to accept keyword arguments #1567

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/kernel.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,7 @@ module Kernel : BasicObject
def self?.fail: () -> bot
| (string message, ?cause: Exception?) -> bot
| (_Exception exception, ?_ToS? message, ?String | Array[String] | nil backtrace, ?cause: Exception?) -> bot
| (_Exception exception, ?cause: Exception?, **untyped) -> bot
Copy link
Member

@soutaro soutaro Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                | (_Exception exception, ?String | Array[String] | nil backtrace, ?cause: Exception?, **untyped) -> bot

🗑️ Withdraw this suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Kernel.raise accepts keyword arguments only if no positional arguments. So it's not good to add backtrace argument here.

irb(main):007> raise Exception, foo: 1
(irb):7:in `<main>': {:foo=>1} (Exception)
	from /Users/tkomiya/.dotfiles/_rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.8.1/exe/irb:9:in `<top (required)>'
	from /Users/tkomiya/.rbenv/versions/3.2.2/bin/irb:25:in `load'
	from /Users/tkomiya/.rbenv/versions/3.2.2/bin/irb:25:in `<main>'
irb(main):008> raise Exception, '', foo: 1
(irb):8:in `set_backtrace': backtrace must be Array of String (TypeError)
	from (irb):8:in `raise'
	from (irb):8:in `<main>'
	from /Users/tkomiya/.dotfiles/_rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.8.1/exe/irb:9:in `<top (required)>'
	from /Users/tkomiya/.rbenv/versions/3.2.2/bin/irb:25:in `load'
	from /Users/tkomiya/.rbenv/versions/3.2.2/bin/irb:25:in `<main>'
irb(main):009> raise Exception, '', [], foo: 1
(irb):9:in `raise': wrong number of arguments (given 4, expected 0..3) (ArgumentError)
	from (irb):9:in `<main>'
	from /Users/tkomiya/.dotfiles/_rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.8.1/exe/irb:9:in `<top (required)>'
	from /Users/tkomiya/.rbenv/versions/3.2.2/bin/irb:25:in `load'
	from /Users/tkomiya/.rbenv/versions/3.2.2/bin/irb:25:in `<main>'
irb(main):010> raise Exception, [], foo: 1
(irb):10:in `set_backtrace': backtrace must be Array of String (TypeError)
	from (irb):10:in `raise'
	from (irb):10:in `<main>'
	from /Users/tkomiya/.dotfiles/_rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.8.1/exe/irb:9:in `<top (required)>'
	from /Users/tkomiya/.rbenv/versions/3.2.2/bin/irb:25:in `load'
	from /Users/tkomiya/.rbenv/versions/3.2.2/bin/irb:25:in `<main>'


# <!--
# rdoc-file=eval.c
Expand Down
5 changes: 5 additions & 0 deletions test/stdlib/Kernel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,11 @@ def test_fail
rescue test_error
end

begin
fail test_error.new('a'), foo: 1, bar: 2, baz: 3, cause: RuntimeError.new("?")
rescue test_error
end

exception_container = Class.new do
define_method :exception do |arg = 'a'|
test_error.new(arg)
Expand Down