Last active
December 30, 2015 08:09
-
-
Save zeroeth/7800651 to your computer and use it in GitHub Desktop.
ruby magic
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 Awesome | |
| attr_accessor :first_var | |
| def second_var= value | |
| @second_var = value; | |
| end | |
| def second_var | |
| @second_var | |
| end | |
| end | |
| awesome = Awesome.new | |
| awesome.first_var = 5 | |
| awesome.second_var = 5 |
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 OtherThing | |
| attr_accessor :ship | |
| def initialize ship | |
| self.ship = ship | |
| end | |
| # individual delegators | |
| def first_var= value | |
| ship.first_var = value | |
| end | |
| def first_var | |
| ship.first_var | |
| end | |
| #... | |
| end |
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 OtherThing | |
| attr_accessor :ship | |
| attr_accessor :first_var, :second_var | |
| #... | |
| def sync_attrs | |
| [:first_var, :second_var].each do |method| | |
| ship.send "#{method}=", self.send method | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment