Recursive Sum
Code: recursion, Ruby
Comments Off on Recursive Sum
In #ruby on Freenode, platzhirsch asked about how to total an array of Transactions when the Transactions may have parents. The two obvious approaches have pitfalls when there are a lot of Transactions, and he said he expects to have 23 million that may be deeply nested. Here’s his sample code:
Have You Seen This Cache?
Code: C, cache, memcached, Python, referential transparency, Ruby
Comments Off on Have You Seen This Cache?
It looks like syntax highlighting, image thumbnails, and compiling object files.
Let me explain.
Sizing Up My Queue
Code: media, podcasts, Ruby, yak shave
Comments Off on Sizing Up My Queue
I have a folder named “queue” that I download podcasts, videos, and books to. It occurred to me that it hasn’t been empty for years. That’s OK, the world is a very interesting place, and I care that I produce things, not just consume them.
But I wondered how big the queue is. Not in terms of disk space, that doesn’t tell me much because the different encoding rates and formats result in very different file sizes. (The disk space command is df -h
, if you need it.) In terms of time.
The Two Kinds of Integers
Code: abstractions, assembly, Haskell, Ruby, values
Comments Off on The Two Kinds of Integers
A small thing I see tripping up developers is that there are two kinds of integers: numbers and identifiers.
We don’t know what the integer 4 means unless we know what it’s for. Are we counting things, or identifying them? It’s really easy to slip between the two, like this real code snippet I found:
Isolating Polymorphism
Code: encapsulation, inheritance, Ruby
Comments Off on Isolating Polymorphism
I’m reading Code Complete, 2nd ed and it’s been like catching up with an old friend. I remember reading the first edition at my first tech job fifteen years ago. At the time a lot of it went by me, but rereading I can see a lot of things I had to learn on my own.
Type-Checking Interface in Ruby
Code: boundaries, experiment, interfaces, POODR, Ruby
Comments Off on Type-Checking Interface in Ruby
One of my favorite parts in Sandi Metz’s excellent Practical Object Oriented Design in Ruby is when she describes how to enforce lightweight interfaces so that multiple objects can play a role reliably. And I thought: how I can I enforce this in a much-more heavy-handed and annoying way?
This question also ties into two ridiculous
tweets of mine:
Builder Methods
Code: domain-driven design, immutability, Ruby, testing, validity
Comments Off on Builder Methods
I was reading Corey Haines’ book Understanding the Four Rules of Simple Design (capsule review on the 2014 book reviews post) when I read:
In fact, over time I’ve developed a guideline for myself that external callers can’t actually use the base constructor for an object. Put another way: the outside world can’t use
new
to instantiate an object with an expectation of a specific state. Instead, there must be an explicitly named builder method on the class to create an object in a specific, valid state.
This was a brief aside at the tail of a longer discussion of interdependent and redundant tests. It really caught my attention and I’d like to hear it more thoroughly investigated. In the hopes of attracting Corey’s attention, I offer this cat pic and an exploration of the benefits of a similar practice I have:
Two Designs for SequenceIdGenerator
Code: Chibrary, design, object orientation, Ruby, state
Comments Off on Two Designs for SequenceIdGenerator
In my previous, marathon post on id generation, I mentioned that I was generating unique sequence numbers. It was straightforward to write a class to generate them:
Inheriting From Built-In Ruby Types
Code: ActiveRecord, refactoring, Ruby, strings
Comments Off on Inheriting From Built-In Ruby Types
2015-05-09: While I still want to break down ActiveRecord models, I now disagree with the idea of inheriting from Ruby’s stdlib types and think they should always be wrapped. My RailsConf 2015 talk expands on the thinking below (especially about immutability!) and touches on reasons why not to inherit from stdlib.