Git Product home page Git Product logo

ruby-life-without-mutations's Introduction

Life in Ruby with No Mutations

This was inspired by the "no mutations" round in the Code Retreat day of Ruby DCamp. I started it while pairing with Kalimar Maia, and of course we didn't finish it, but it piqued my interest so I finished it later. (No, I didn't test-drive the rest of it.)

To run it, go into irb, load it, and run World.run. By default, this will give you a world of 80 columns and 20 rows, with a delay of 1/20 of a second between iterations. You can pass it a hash of options, including (each being optional):

  • cols: the number of columns
  • rows: the number of rows
  • delay: delay in seconds between iterations (yes, 0 is OK)

Each iteration will show the state of the world (or rather, show the current world), with its number and how many cells it contains. Once it has reached stability (defined as the same set of cells as sometime in the past 15 iterations), it will halt, reporting the cycle length and number of iterations per second.

You could also run it in a loop, with a command like: while true ; World.run ; sleep 1 ; end (inlcuding your desired options).

Notes:

  • This relies on ANSI escape sequences, so if its output looks like a load of garbage, go figure out how to turn those on for whatever sort of system you're using. Don't ask me.

  • Since Ruby does not do tail-recursion optimization (at least by default, and I didn't want to bother turning it on), you may run out of stack space, but it will quite some time. I have not hit the wall even after many runs at 132 columns by 66 rows.

  • Yes, it does I/O, which is mutating the state of the screen. If you're inclined to quibble about this, go stick your head in a pig.

ruby-life-without-mutations's People

Contributors

davearonson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.