Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

No need for !! here. #2295

Merged
merged 1 commit into from
Jul 18, 2016
Merged

Conversation

myronmarston
Copy link
Member

Follow up to #2294.

@@ -21,7 +21,7 @@ def filter_applies?(key, filter_value, metadata)

meta_value = metadata.fetch(key) { return false }

return true if TrueClass === filter_value && !!meta_value
return true if TrueClass === filter_value && meta_value
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated, but Is there a reason why this isn't filter_value == true?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because filter_value could be anything, and we can't count on it's == being defined properly. We've repeatedly gotten bug reports from users when we've called == on their objects where they defined == to assume the passed object was of the same type, and tried to access specific attributes on it that did not exist. Calling a method on TrueClass instead of filter_value avoids that issue.

It could be true == filter_value but TrueClass === is in keeping with the style of the rest of the method which repeatedly uses SomeClass === value.

@fables-tales
Copy link
Member

LGTM, but I did have a question.

@fables-tales
Copy link
Member

Right you are, that makes lots of sense. Thanks!

@myronmarston myronmarston merged commit 7ef58fa into master Jul 18, 2016
@myronmarston myronmarston deleted the myron/simplify-metadata-filtering-further branch July 18, 2016 17:49
@mrageh
Copy link
Contributor

mrageh commented Jul 19, 2016

@myronmarston why do we not need !!, I thought the return type of meta_value was a non-boolean value?

For more context on the original conversation I had with @JonRowe please refer to #2294 (comment)

@JonRowe
Copy link
Member

JonRowe commented Jul 19, 2016

As I was trying to explain over on #2294, it's not needed because we only care if meta_value is truthy.

e.g. if 'string' evaluates to true the same as if !!'string'

!! is typically use to convert a value into a boolean, for example if we were returning the value:

def is_metadata_true?(key)
  !!key
end

def is_metadata_truthy?(key)
  key
end

@mrageh
Copy link
Contributor

mrageh commented Jul 19, 2016

Thank you @JonRowe you've clarified it to me now, originally I was confused because I did'nt consider truthy values being eq to true in Ruby.

MatheusRich pushed a commit to MatheusRich/rspec-core that referenced this pull request Oct 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants