Skip to content

Instantly share code, notes, and snippets.

@IceDragon200
Created October 22, 2014 17:12
Show Gist options
  • Save IceDragon200/b661d9a439cd1679d7c6 to your computer and use it in GitHub Desktop.
Save IceDragon200/b661d9a439cd1679d7c6 to your computer and use it in GitHub Desktop.
Attribute flopping
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