Created
February 17, 2016 00:26
-
-
Save theHamdiz/96013379c49744336133 to your computer and use it in GitHub Desktop.
Use the #tap method to initialize your objects beautifully and elegantly instead of setting various properties manually.
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 User | |
| attr_accessor :a, :b, :c | |
| end | |
| # basically the tap method yields the calling object | |
| # to the block and returns it back | |
| user = User.new.tap do |u| | |
| u.a = 1 | |
| u.b = 2 | |
| u.c = 3 | |
| end | |
| p user |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment