This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 102
Recognize hash as optional arg when optional keyword is present #366
Merged
JonRowe
merged 3 commits into
rspec:master
from
edzhelyov:recognize-hash-as-optional-argument
Mar 14, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,14 +77,19 @@ def invalid_kw_args_from(given_kw_args) | |
given_kw_args - @allowed_kw_args | ||
end | ||
|
||
# If the last argument is Hash, Ruby will treat only symbol keys as keyword arguments | ||
# the rest will be grouped in another Hash and passed as positional argument. | ||
def has_kw_args_in?(args) | ||
Hash === args.last && could_contain_kw_args?(args) | ||
Hash === args.last && | ||
could_contain_kw_args?(args) && | ||
args.last.keys.any? { |x| x.is_a?(Symbol) } | ||
end | ||
|
||
# Without considering what the last arg is, could it | ||
# contain keyword arguments? | ||
def could_contain_kw_args?(args) | ||
return false if args.count <= min_non_kw_args | ||
|
||
@allows_any_kw_args || @allowed_kw_args.any? | ||
end | ||
|
||
|
@@ -357,7 +362,14 @@ def unlimited_args? | |
|
||
def split_args(*args) | ||
kw_args = if @signature.has_kw_args_in?(args) | ||
args.pop.keys | ||
last = args.pop | ||
non_kw_args = last.reject { |k, _| k.is_a?(Symbol) } | ||
if non_kw_args.empty? | ||
last.keys | ||
else | ||
args << non_kw_args | ||
last.select { |k, _| k.is_a?(Symbol) }.keys | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you saying with this that Ruby will mix keyword args and hash keys? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you provide an anonymous Hash as the last argument only the symbols from that argument will be treated as keyword arguments, the rest will be interpreted as positional one.
I'm trying to cover the behavior in this spec |
||
end | ||
else | ||
[] | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.