Skip to content
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

Merged

Conversation

prathamesh-sonpatki
Copy link
Member

Summary

r? @rafaelfranca

# { c: true }.compact # => { c: true }
def compact
select { |_, value| !value.nil? }
unless self.instance_methods(false).include?(:compact)
Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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.

@vipulnsward
Copy link
Member

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

@prathamesh-sonpatki
Copy link
Member Author

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
Copy link
Member

vipulnsward commented Oct 23, 2016

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.

- 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.
@seuros
Copy link
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
@prathamesh-sonpatki prathamesh-sonpatki deleted the use-hash-compact-from-ruby-24 branch October 25, 2016 05:03
@vtrkanna
Copy link

Is this part of ruby 2.4 or rails 5 ?

@rafaelfranca
Copy link
Member

both

@kaspth
Copy link
Contributor

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
Copy link
Member Author

Ruby 2.4

@kaspth
Copy link
Contributor

kaspth commented Oct 25, 2016

Jinx! @prathamesh-sonpatki @rafaelfranca 😁

matthewd pushed a commit to matthewd/rails that referenced this pull request Dec 26, 2016
…act-from-ruby-24

Use Hash#compact and Hash#compact! from Ruby 2.4
matthewd pushed a commit to matthewd/rails that referenced this pull request Dec 27, 2016
…act-from-ruby-24

Use Hash#compact and Hash#compact! from Ruby 2.4
y-yagi added a commit to y-yagi/rails that referenced this pull request Jan 6, 2017
`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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants