Created
May 16, 2017 11:10
-
-
Save pocke/a592e591fe58b9ae9a0f044c432a8320 to your computer and use it in GitHub Desktop.
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
| class NilClass | |
| def if | |
| self | |
| end | |
| def else | |
| yield self | |
| end | |
| end | |
| class FalseClass | |
| def if | |
| self | |
| end | |
| def else | |
| yield self | |
| end | |
| end | |
| class Object | |
| def if | |
| yield self | |
| end | |
| def else | |
| self | |
| end | |
| end | |
| class User | |
| def self.find_by(id:) | |
| if id == 1 | |
| self.new | |
| else | |
| nil | |
| end | |
| end | |
| end | |
| User.find_by(id: 1).if do |user| | |
| p user # => print user | |
| end | |
| User.find_by(id: 2).if do |user| | |
| p user # => not print | |
| end.else do |user| | |
| p user # => print flase | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
&.tap