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
Use Hash#compact and Hash#compact! from Ruby 2.4 #26868
Use Hash#compact and Hash#compact! from Ruby 2.4 #26868
Conversation
# { c: true }.compact # => { c: true } | ||
def compact | ||
select { |_, value| !value.nil? } | ||
unless self.instance_methods(false).include?(:compact) |
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 should be an explicit check on Hash.instance_methods(false)
, not sure how the behaviour would be with Hash
inherited classes.
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.
What is the difference between this and explicit check?
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.
class Hash
def puts_self
puts "Self#{self.class}"
end
end
class HashImp < Hash
end
HashImp.new.puts_self
=> SelfHashImp
The self picks up the current class. Again, not sure about the implications, its better to be explicit.
Needs CHANGELOG like vipulnsward@7ad4690 |
I did not add changelog because this change is compatible with AS. So no change in public consumption of these methods unlike Enumerable#sum. |
Ideally it would good that they are same, but if there are any differences/bugs that we find in future, it is good to mention what has changed. |
1bb06cd
to
27b3ab0
Compare
- Ruby 2.4 has added Hash#compact and Hash#compact! so we can use it now. - Reference: https://bugs.ruby-lang.org/issues/11818 and https://bugs.ruby-lang.org/issues/12863.
27b3ab0
to
69a3fa1
Compare
@prathamesh-sonpatki I think we can test just once , because the language either implement both or none. unless Hash.instance_methods(false).include?(:compact)
class Hash
def compact
# code here
def compact!
#code here.... |
Is this part of ruby 2.4 or rails 5 ? |
both |
Active Support has shipped with |
Ruby 2.4 |
Jinx! @prathamesh-sonpatki @rafaelfranca 😁 |
…act-from-ruby-24 Use Hash#compact and Hash#compact! from Ruby 2.4
…act-from-ruby-24 Use Hash#compact and Hash#compact! from Ruby 2.4
`Hash#compact` of Ruby native returns new hash. Therefore, in order to return HWIDA as in the past version, need to define own `#compact` to HWIDA. Related: rails#26868
Summary
now.
r? @rafaelfranca