Last week I finally implemented something, an elementary cellular automaton with one hardcoded rule. I also added some development tools (debugger, testing framework). Not much, but I’m happy that I could see some results and now I have some base for next iterations.
More setup
Debugging
Pry – debugger and powerful tool, is a popular choice for development in Ruby.
To use it, we just need to add in Gemfile this: gem 'pry'
, and then this line in code where we want a break point:
require 'pry'; binding.pry
Testing
RSpec – when it comes to testing (especially in Rails apps), this is the most used framework and DSL. After adding gem 'rspec'
in Gemfile, we can generate initial config for it:
$ rspec --init create .rspec create spec/spec_helper.rb
There is good RSpec documentation and guidelines on the web.
Environment groups
In Sinatra default environment is “development” and since there is no config for that – all dependencies are required in every environment. In order to fix this and also
to introduce “test” environment I needed to make some configuration tweaks.
For testing, I’ve added this line on top of spec/spec_helper.rb
:
ENV['RACK_ENV'] = 'test'
For dependencies, we can group them in Gemfile as described in Bundler docs.
To require gems in particular group I’ve added this line on top of config.ru
:
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
Implementation
Elementary (one-dimensional) cellular automaton is really easy to code in Ruby. My first implementation has 40 lines of code. It’s a class with few instance methods. Object created from it can hold a generation in an array. Elements of an array are cells, their state is defined by integer (0 or 1). Method named “step” (no idea if it’s good name) returns new array generated basing on neighborhood of cells and some rule, in this case rule 30. (more…)