-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Fixed incorrect call for .map on a hash
#51868
Conversation
There was a mistake in the method implementation where coder.map is called instead of directly using coder.key?("value")
Can you please explain why this code was failing, or write a test showing the problem? |
@@ -124,15 +127,15 @@ def hash | |||
[self.class, name, value_before_type_cast, type].hash | |||
end | |||
|
|||
def init_with(coder) | |||
def init_with(coder = EMPTY_HASH) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should never be called without an argument, so why this change?
@@ -153,7 +156,9 @@ def original_value_for_database | |||
alias :assigned? :original_attribute | |||
|
|||
def initialize_dup(other) | |||
if @value&.duplicable? | |||
# Using `defined?(@value)` ensures that it only proceed if `@value` is actually set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I get his. Why wouldn't @value
be set?
@name = coder["name"] | ||
@value_before_type_cast = coder["value_before_type_cast"] | ||
@type = coder["type"] | ||
@original_attribute = coder["original_attribute"] | ||
@value = coder["value"] if coder.map.key?("value") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mapper can't be an enumerable. It is always an hash. See https://github.com/ruby/psych/blob/master/lib/psych/coder.rb#L14
The more I look at those changes, the more I think they are wrong. Perhaps you are miss using For example, the YAML::Coder doesn't have a |
Motivation / Background
This Pull Request has been created because There was a mistake in the method implementation where
coder.map.key?
is called instead of directly usingcoder.key?("value")
The error was causing method 'key?' for #<Enumerator: {}:map> indicates thatcoder.map
returns an enumerator, which does not have akey?
Used a constant for
coder
args as an empty hash, this is better for optimizing memory usage and ensuring immutability, especially in scenarios where the method might be called frequently without arguments.Detail
This Pull Request changes:
set default value for coder argument in each of the following methods:
Updated the following line to reduce memory allocation.
Added a value check in
initialize_dup(other)
Additional information
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]