Counting Fruit in Rust, Part 2

This is the third part of a series on beginning rust. You may wish to read part 1 and part 2 first.

I want to be able to visit all the nodes of a tree in Rust. Last time, I showed the implementation of the tree itself, and code that would generate a random tree. This time, I’m going to show and describe code for walking around a tree and counting which nodes have a flag set. The code we’ll talk about this episode is here: Continue reading “Counting Fruit in Rust, Part 2”

Counting Fruit in Rust

Last time, I mentioned that I was starting up Rust again, and mentioned some helpful videos and thoughts that will help me as I take another swing at learning this language. The complete source for the examples I’ll use today is on GitHub, and you should be able to download it and cargo run without much trouble. If you don’t have Rust and Cargo, you want to install rustup first.

Today, we’re going to generate a random tree, then we’re going to walk the tree recursively and count which nodes have a flag set. Here’s the code for representing the nodes and generating the tree: Continue reading “Counting Fruit in Rust”

Starting Rust (again)

Last time I messed with Rust, I made the classic beginner mistake of trying to learn too many things at once. I was trying to learn Rust, learn Lisp, and write a Lisp interpreter in Rust at the same time.

I started messing with Rust again. First, I want to mention a couple of videos that have really helped me understand Rust a little bit better.

Continue reading “Starting Rust (again)”

Watch for Mutants

I have a project at work that uses Minitest, but I want to use mutant to test it. Mutant is a gem that does mutation testing. Mutation testing is an important complement to regular automated testing. Regular automated testing asserts that correct code passes your test suite; mutation testing asserts that incorrect code fails your test suite. In this series, I’m going to try to find the best way to mutation test a Rails app that’s covered mostly be minitest.

Continue reading “Watch for Mutants”

Don’t YAML on, man (in Rails 4)

Last time, I made a simple model called Lightswitch so that I could play with mutable state:

class Lightswitch < ActiveRecord::Base
  attr_accessor :turned_on
  def turn_on
    self.turned_on = true

When I first wrote the model, not wanting to suggest that my electrical fixtures were aroused, I named the attribute on instead of turned_on. I immediately saw some strange errors. rake test started complaining that there was no column named true. Today, I’m going to reproduce this error on purpose so that I will understand why, specifically, I can’t use on as a column name with active record. Continue reading “Don’t YAML on, man (in Rails 4)”

State and Caching in Minitest

Last time, I learned that using let blocks in minitest cause minitest to define a method on the test class, and I hypothesized that this was to allow tests to share code while preventing them from sharing state. But the method that gets defined caches the results, so that the initializing block is only evaluated on the first call. I want to convince myself that test methods cannot corrupt this cache, or rather, can’t corrupt this cache without doing so on purpose. Continue reading “State and Caching in Minitest”

Throwing Darts at Code

I’ve been thinking about code reviews a lot lately.

My mom has often worked as an editor, and she once told me of the most difficult thing about being an editor (I paraphrase):

You have to let the writer keep his or her style. The challenge is to improve the writing without making it seem like you wrote it.

Working as a technical writer and as an English teacher, I thought about these things a lot. I would often think, “that is not a sentence that I would have written, but it is clear. It works.” Sometimes I would read a sentence and think, “I don’t know what that meant; that sentence doesn’t work.” I would change the sentence in the second situation but not the first. Continue reading “Throwing Darts at Code”