Created
October 22, 2014 17:12
-
-
Save IceDragon200/b661d9a439cd1679d7c6 to your computer and use it in GitHub Desktop.
Attribute flopping
This file contains 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
def attr_flop(obj, attribute) | |
old_value = obj.send(attribute) | |
yield obj, old_value | |
new_value = obj.send(attribute) | |
obj.send("#{attribute}=", old_value) | |
new_value | |
end | |
class Camera | |
attr_accessor :position | |
end | |
cam = Camera.new | |
new_position = attr_flop(cam, :position) { cam.position += [1, 2, 0] } | |
# camera retains its original position, and you are still able to retrieve its new position | |
attr_flop(cam, :position) { frame_update(delta) } # camera is modified heavily during the frame update |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment