Troubleshooting a basic docker issue: Getting a script into the container

Sometimes when I’m working with docker, I’ll have a frustrating experience. I want to do something simple, like copy a shell script into the container and execute it as the container’s entry point, but I miss a silly detail and get stuck for a while. The other day I was trying to run a dockerized Redis instance, and I kept running into a silly permissions issue with the file system that I just couldn’t figure out. In retrospect, as is so often the case in software, I was testing elaborate hypotheses and not checking something obvious, but in case this post saves someone else the trouble, I wanted to go into a little detail. Here’s how to make a redis container that starts with a shell script instead of just starting up the redis server right away. Continue reading “Troubleshooting a basic docker issue: Getting a script into the container”

Querying and Enumeration

Querying a collection is interesting in that, basically, all the operations are built on enumeration. Filtering an array? Enumerate it, and reject elements that don’t meet the predicate. Transforming an array? Enumerate it, and apply the transformation function to each element. Reducing an array (e.g., summing up its elements)? Enumerate it, and keep a running total as you add up each element. That means that, if the language and library desingers were clever, just having enumeration on a class is enough to make it queryable with the full filter/map/reduce set of array operations. This means that, in many languages, it’ possible to write custom collections that add a lot of functionality just by implementing enumeration on our class.

Today we’re going to write two classes, Menu and MenuItem, in both C# and Ruby, and look at how we can tell the code to treat Menu as a collection of MenuItems and then query it. (Also, this post was inspired by another)

In C#, we’re going to use LINQ (the strangely named Language-Integrated Query library that gives C# map/reduce like functionality), and we’ll use the Ruby module Enumerable. Both programs will do the same thing: Look at a list of menu items and filter them based on allergen information. Let’s start with C#.

To get a class in C# to work with LINQ, we have to implement the IEnumerable<T> interface. The C# interface IEnumerable<T> just exposes one public method: GetEnumerator(). There’s one historical accident that, in my estimation, mars otherwise very clean implementation: To implement IEnumerable<T>, we have to implement IEnumerable. In other words, because the non-type-safe IEnumerable was around before the type-safe IEnumerable, we need to use both. Here’s the implementation:

On line 8, we declare that we want this class to be enumerable. Then on line 17, we implement a very simple enumerator. In this case, we just return the enumerator of the underlying list. Line 22 is the blemish I alluded to earlier: because C# 1.0 didn’t have generics, parts of the language rely on the IEnumerable interface, rather than on the IEnumerable<T> interface, so we have to implement both. In this case, we can just return the same enumerator in response to both methods. (The difference is that clients calling the second method will see a collection of Object, not a collection of MenuItem, and will have to cast at runtime.)

That let’s us make this happy little snippet:

var okToEat = menu
              .Where(item => !item.Allergens.Contains("shellfish"))

Now let’s look at the same thing in Ruby:

This essentially the same class. The important line is line 2: include Enumerable. This adds Ruby’s Enumerable module into the class, which keeps us from getting NoMethoErrors if we call each or filter. Consuming this ruby class looks like this:

ok_to_eat = menu.reject { |item| item.allergens.include? 'shellfish' }

It’s also worth noting that both Ruby and C# will now let you use a Menu in a regular for each loop. I think this can be a handy, if little-used feature in both languages. Here are some reasons why you would do this:

  1. You need to represent an object in your business domain that logically is a collection of some kind.
  2. You want to implement a data structure that didn’t come with the base class libraries, and you want to be able to enumerate that data structure.
  3. You want to make a data structure that clients can query but cannot modify.

I also want to point out some interesting similarities: First, even though Ruby and C# are very different languages, they’ve both constructed enumeration and querying collections in such a way that it can be achieved in user-defined classes just by implementing one method: GetEnumerator() in C# and #each in Ruby. They have also both implemented their native for each loops in such a way that user-defined collections can be used in them. So thanks, language designers for this cool thing.

Till next time, happy learning!


Acknowledgments: A few things gave me the idea for this post: First, in an episode of .NET Rocks, though I can’t find the specific episode, Carl said simply, “If you can enumerate a collection you can query it.” Second, I went to a Meetup talk in DC where the speaker ran through a number of different programs that can be written in JavaScript using only map, filer, and reduce. Also, I should thank Jon Skeet for writing the excellent C# in Depth, without which I would not understand C# well enought to write posts like this.

Bones and Documents

I’ve been thinking about tests and legacy code. The topic came up when I was listening to an episode of the Legacy Code Rocks podcast with the famous Michael Feathers, author of Working Effectively with Legacy Code. Andrea Goulet, one of the podcast’s hosts, and co-founder Corgibytes, a company that specializes in legacy code, made the observation that when working in legacy code, tests are like bones: They are very important to archeologists, and they stick around.

The analogy of legacy code work to archeology is a good one, but I’m not sure about the analogy between tests and bones. On one hand, bones are very important to archeology. They last a long time (or, as academics would say, they “persist in the archeological record”), and they can be tied to a particular species. But I think tests have a more important aspect than durability. The important thing about tests is that they were written down by their authors. Reading tests is more like finding documents.

When I was studying ancient literature and history in college, there was one glorious semester when I was taking a class in Roman Comedy, and a class in archeology of Roman domestic life. Once, we were having a debate in the archeology class about how private different parts of the house were, asking questions like: “guests are allowed in the atrium, but would it be awkward if some guest wandered into the kitchen?”

This type of question is difficult to answer from an archeological perspective. People have done studies like count the number of doors a person has to pass through from the street to get to a particular room, and used that as a proxy for how “private” the room is considered, but that’s a bit of a guess. Then, when I was reading Roman Comedy, I found a passage where a man starts complaining about how unwanted visitors are trampling all over his privacy because they won’t stay out of the kitchen. In my mind, that answers the question definitively: The way we know Romans felt like the kitchen was more private than the atrium is that one of them makes a joke about unwelcome visitors who aren’t polite enough to stay out of the kitchen. Mystery solved.

Going back to software for a moment: Tests, even bad or very old tests, tell us about what the authors of a system believed at the time that the system actually did. I think legacy code can be approached from two perspectives, much like the Roman house: We can do archeology or we can look at documents. Archeology is reading the source code itself, doing little refactorings, running static analyzers, figuring out when and whether a given method is called. Document-finding is about tests and documentation. If there’s a test that says test "the autoload module works like normal autoload" do (from Rails), we may not love the descriptor, but we know that the previous developers thought autoloading was a normal thing that people do, and that the code in the test represented normal autoloading. That’s a valuable piece of information. I think in terms of archeology, it’s definitely document-type evidence, not artifact-type evidence.

Let me try to tie the analogy all back together: Archeology and legacy code investigation are similar in that they both involve trying to discover more about some artifact that previous people left. In both cases, we generally can’t ask the original creators of the author what they wrote, either because they don’t work at that company any more, or because they’ve been dead for thousands of years. Legacy code investigation and (recent) archeology both have two primary modes of investigation: By inspecting the artifacts themselves, and by inspecting documents that were produced by the same people as produced the artifacts. Both modes have strengths and weaknesses, but they work well together in concert.

So next time you’re trying to understand some old piece of code that ancient peoples (or you 2 months ago) wrote, remember to examine the artifacts (code itself) and the documents (tests and commit messages) together. And check out the next episode of Legacy Code Rocks!

Till next time, happy learning!


Custom Indexers: Ruby vs C#

In C#, I can make a custom indexer. In other words, I can write my own class that will let me say things like var x = myClass[foo]; and have my own code run when we “index” into myClass with foo. In Ruby, I was wondering how to do this. I knew that I could subtype Array or Hash and get something like this, but (1) that might not be best practice, and (2) I want to know whether [] and []= are built into the language, or I can define their behavior myself. As is often the case with Ruby, this feature is wide open and I can override or modify this behavior. In order to illustrate this, I decided to make a case insensitive dictionary in both Ruby and C#. This dictionary will use strings as its keys, and will return the value associated with a string in any casing. (Note that this is not a dictionary I have a real use case for; I am just building it as an experiment.) Continue reading “Custom Indexers: Ruby vs C#”

CTF is Awesome

Last Saturday I attended my very first computer security Capture the Flag (CTF). This event consisted of teams competing in computer security related challenges to score points. Generally, at the end of the challenge, the team has caused the system we’re attacking to give up a file that says “This is the flag:” and has a long random number. Submitting the flag scores a point, hence “capture the flag.”

First things first: This was a great time, and I highly recommend attending events like this. I came without a team, sat at a table of strangers, and had a great time. Today I’ll talk about what the event was like. Continue reading “CTF is Awesome”

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”