Created
February 16, 2018 16:15
-
-
Save suginoy/105d2e32a9c3d69765a737b9383cb666 to your computer and use it in GitHub Desktop.
my extension of Object class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support' | |
require 'active_support/core_ext' | |
class Object | |
# # my Object#survive = ActiveSupport's Object#presence + block | |
def survive | |
if present? | |
if block_given? | |
self if yield(self) | |
else | |
self | |
end | |
end | |
end | |
end | |
# example | |
1.survive(&:even?) # => nil | |
2.survive(&:even?) # => 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment