You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A mixin to parse "spintax", a text format used for automated article generation. Can handle nested spintax, and can count the total number of unique variations. Now also supports consistent unspinning!
Run the code above, and you will end up with several random variations of the same text, such as:
Hi worlds.
Hi planet?
Hello world?
Hi planet?
Hi world?
Hi world!
Hi world.
Hello world.
Hello world!
Hello worlds.
And don't worry: calling #unspin on a string with no spintax will safely return an unaffected copy of the string.
Also, note that the #unspin method doesn't really care if the class you mix it into is a descendant of String or not, as long as its #to_s method returns a string written in spintax.
Consistent unspinning
Got a special project that requires unspinning the same spintax the same way in certain circumstances? No problem. If you're using a Ruby version >= 1.9.3, you can pass a pre-seeded random number generator to the #unspin method just like you would to the Array#sample method. Et voila! Consistent unspinning!
You can also count the total number of unique variations of a spintax string. If you've mixed the SpintaxParser into your String class like above, just call the #count_spintax_variations method on any string as shown below: