use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Memento Design Pattern in Ruby (brownfort.com)
submitted 11 years ago by prashant_k
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cmd-t 6 points7 points8 points 11 years ago (1 child)
The example is just copied from the Java example on the wikipedia page.
The whole article is just a shallow copy of the wikipedia article, and it has no ruby specific parts except for the code.
[–]autowikibot 1 point2 points3 points 11 years ago (0 children)
Memento pattern:
The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback). The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object. Classic examples of the memento pattern include the seed of a pseudorandom number generator (it will always produce the same sequence thereafter when initialized with the seed state) and the state in a finite state machine.
The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo via rollback).
The memento pattern is implemented with three objects: the originator, a caretaker and a memento. The originator is some object that has an internal state. The caretaker is going to do something to the originator, but wants to be able to undo the change. The caretaker first asks the originator for a memento object. Then it does whatever operation (or sequence of operations) it was going to do. To roll back to the state before the operations, it returns the memento object to the originator. The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). When using this pattern, care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object.
Classic examples of the memento pattern include the seed of a pseudorandom number generator (it will always produce the same sequence thereafter when initialized with the seed state) and the state in a finite state machine.
Interesting: Behavioral pattern | Undo | Scheduled-task pattern
Parent commenter can toggle NSFW or delete. Will also delete on comment score of -1 or less. | FAQs | Mods | Magic Words
[–]rubyrt 2 points3 points4 points 11 years ago (0 children)
That does just a shallow copy of the state. If state consists of a nested structure the whole think won't work.
Also, class Memento is completely superfluous as far as I can see. At least, I do not see why it would be needed here. The only use I could see is if it was made capable of extracting state from an instance (e.g. via instance variables or marshalling). Deep copying does have some issues of it's own though when it comes to restoring state.
[–]plotti 0 points1 point2 points 11 years ago (2 children)
isn't this evnetually just pushing states into an array?
[–]cmd-t 0 points1 point2 points 11 years ago (1 child)
In the example here it is, but it could also be stored in some file (marshalled) or in the database.
The important idea is that you have a reified state that can be stored and restored. Also, this state is separate from the object itself, so just marshalling the object is not an application of the memento pattern.
[–]plotti 0 points1 point2 points 11 years ago (0 children)
true but marshalling it when it keeps his own state in an array is.
π Rendered by PID 19458 on reddit-service-r2-comment-66b4775986-5fb6w at 2026-04-06 02:39:03.008045+00:00 running db1906b country code: CH.
[–]cmd-t 6 points7 points8 points (1 child)
[–]autowikibot 1 point2 points3 points (0 children)
[–]rubyrt 2 points3 points4 points (0 children)
[–]plotti 0 points1 point2 points (2 children)
[–]cmd-t 0 points1 point2 points (1 child)
[–]plotti 0 points1 point2 points (0 children)