O'Reilly logo

Ruby by Example by Kevin C. Baird

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

#35 Mutating Strings into Weasels (methinks.rb)

This script is based on a program from Richard Dawkins’ The Blind Watchmaker (W.W. Norton, 1996).The program demonstrates a simplified model of asexual natural selection, starting with a String consisting of random characters and successively mutating it to produce “children” that differ from the parent. The program then selects the “best” child String (meaning the one that most closely matches the target String methinksitislikeaweasel, a reference from Hamlet) to be the next generation’s parent. This process continues until the parent String matches the target String.

Let’s implement Dawkins’ process in Ruby.

Note

Dawkins wrote his program to demonstrate a version of cumulative selection over time ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required