-
Flattening Nested Hashes in Ruby
Sometimes, especially when working with external data, you will be handed a thickly nested blob of JSON, represented in Ruby as a nested hash. I have found in my experience that processing this data is often simpler when we can work with a flat hash instead, collapsing the key paths into a simple array key. And so, I wrote a function that does just this – it flattens a nested hash into a flat hash.
Continue reading … -
Expecting Exceptions in Ruby
Sometimes in Ruby code, having to use the
Continue reading …begin ... rescue .. end
construction to capture and deal with exceptions can feel overly burdensome. I recently found myself in such a situation and wrote a small function to make my code read a bit more elegantly. -
Safely Accessing Values from Nested Hashes (again)
Implementing a companion method to
Continue reading …Hash#dig
that always returns a value and never throws an error and allows for a default return value. -
Basic RESTful Filtering with Rails
Filtering a collection of data objects into a subset is one of the most common tasks in data-management applications. While well-tread, this problem space provides rich opportunity for exploring and learning about how to implement RESTful, maintainable, flexible, and robust Rails applications. In this post I want to implement a simple filtering feature, walking through the various aspects at the model, controller, and view layers.
Continue reading … -
An interlude with minitest/autorun
An interlude in a series of posts laying out the process, step by step, of building an interpreter in Ruby for working with propositional logic. In this small post, we take our hand-rolled "tests" and move the code into an executable test script with
Continue reading …minitest/autorun
. -
Proper Propositional Logic
The second in a series of posts laying out the process, step by step, of building an interpreter in Ruby for working with propositional logic. In this second post, we expand the interpreter to handle the full range of valid expressions in classical propositional logic.
Continue reading … -
Starting Simple
The first in a series of posts laying out the process, step by step, of building an interpreter in Ruby for working with propositional logic. In this first post, we build an interpreter for working with simple logical expressions and dig into the specifics of the parts of an interpreter as well as the basics of propositional logic.
Continue reading … -
Time in Ruby and ActiveRecord
How does the Ruby
Continue reading …Time
class relate to the ActiveRecordtime
column type? -
Typecasting in Ruby and Rails
I recently had the need to typecast string values passed as query parameters to a controller action to their appropriate type. In solving this problem, I've learned a lot about Rails' typecasting layer, Ruby's typecasting methods, as well as a handful of edge cases. The result was a typecasting function that I think has a lot to offer.
Continue reading … -
A Simple Tree Building Algorithm
It is, unfortunately, not that often that I get the opportunity to devise an algorithm to solve a problem at work. Most work simply doesn’t require that kind of thinking. But I thoroughly enjoy that kind of thinking, and thus thoroughly enjoyed the most recent opportunity I had to employ it. The problem was simple (though I have simplified and abstracted it for this post as well): we have a database table of
Continue reading …things
, and thesethings
have a parent-child hierarchy, and we need to display a tree of thesethings
in our UI. So, let’s dig in.