Use Hash#compact and Hash#compact! from Ruby 2.4 #26868

Merged
merged 1 commit into from Oct 25, 2016

Projects

None yet

6 participants

@prathamesh-sonpatki

Summary

r? @rafaelfranca

- # { c: true }.compact # => { c: true }
- def compact
- select { |_, value| !value.nil? }
+ unless self.instance_methods(false).include?(:compact)
@vipulnsward
vipulnsward Oct 23, 2016 Member

This should be an explicit check on Hash.instance_methods(false), not sure how the behaviour would be with Hash inherited classes.

@prathamesh-sonpatki
prathamesh-sonpatki Oct 23, 2016 Member

What is the difference between this and explicit check?

@vipulnsward
vipulnsward Oct 23, 2016 Member
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.

@vipulnsward
Member

Needs CHANGELOG like vipulnsward@7ad4690
to indicate the change it ruby 2.4+

@prathamesh-sonpatki

I did not add changelog because this change is compatible with AS. So no change in public consumption of these methods unlike Enumerable#sum.

@vipulnsward
Member
vipulnsward commented Oct 23, 2016 edited

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.

@prathamesh-sonpatki prathamesh-sonpatki Use Hash#compact and Hash#compact! from Ruby 2.4
- 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.
69a3fa1
@seuros
Member
seuros commented Oct 24, 2016

@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....
@rafaelfranca rafaelfranca merged commit 3cc30db into rails:master Oct 25, 2016

2 checks passed

codeclimate Code Climate didn't find any new or fixed issues.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@prathamesh-sonpatki prathamesh-sonpatki deleted the prathamesh-sonpatki:use-hash-compact-from-ruby-24 branch Oct 25, 2016
@vtrkanna

Is this part of ruby 2.4 or rails 5 ?

@rafaelfranca
Member

both

@kaspth
Member
kaspth commented Oct 25, 2016

Active Support has shipped with Hash#compact since 4.2, this just uses Ruby's built in methods when they come out in 2.4 (so Ruby upgrading users will get the faster version).

@prathamesh-sonpatki

Ruby 2.4

@kaspth
Member
kaspth commented Oct 25, 2016

Jinx! @prathamesh-sonpatki @rafaelfranca 😁

@y-yagi y-yagi added a commit to y-yagi/rails that referenced this pull request Dec 17, 2016
@y-yagi y-yagi ensure `#compact` of HWIDA to return HWIDA
`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: #26868
31a8354
@y-yagi y-yagi added a commit to y-yagi/rails that referenced this pull request Dec 17, 2016
@y-yagi y-yagi ensure `#compact` of HWIDA to return HWIDA
`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: #26868
e30a4b4
@y-yagi y-yagi added a commit to y-yagi/rails that referenced this pull request Jan 6, 2017
@y-yagi y-yagi ensure `#compact` of HWIDA to return HWIDA
`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: #26868
d97ba34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment