Gray Soft

1

Before getting started, I feel compelled to point out that my dictionary defines a critique as "a detailed analysis and assessment of something." It seems like we often assume the worst of that word, but that's not how I intend it here.

The Ruby community seems to be talking about style guides lately. So let's talk about them.

The fact is that you will have many choices if you go looking for style guides for our favorite language. You can pick from:

It's obvious that Rubyists care about this topic. Let's see what's out there and consider what really is and is not useful from these guides.

What is a style guide really, and why do we even have them?

Defining style guides is surprisingly tough. I suspect they started out as formatting rules for code, but they have evolved pretty far beyond that now.

Most guides include general conventions that the author feels are important when writing the language in question. This can go all the way down to how to use certain constructs, opinions on what the author considers idiomatic, and syntax to outright avoid.

21

The road to mastering Ruby is paved with understanding some key Ruby concepts. Mix-ins are one of those concepts. I'm sure everyone reading this knows the mechanics of how mix-ins work, but it pays to spend some time really thinking about all that mix-ins imply. Let's do just that.

Adding a Type

One of the primary reasons that Ruby needs mix-ins is that it does not support multiple inheritance. That leaves mix-ins as our only option for modeling hybrid objects. It's the way Ruby programmers can add another type.

That's a good way to think about it too: adding a type.

Take pagination, for example. Pagination methods are usually defined to return an object like this:

First, inheriting from Ruby's core classes can come back to bite you in some scenarios. The reason is that Ruby makes some performance tradeoffs to keep the core classes fast, but those tradeoffs mean that those classes don't always perfectly follow Ruby's rules.

11

I usually stick to pretty code heavy topics in these articles, but please allow me to take a detour this time. Our industry struggles with a problem that we don't discuss enough and I want to give it some air time.

The fact is, we're pretty lousy at controlling stress.

Let's look at why that is and some of the ways this problem manifests. Remember, the first step is admitting that we have a problem.

We are a Passionate People

I really believe good programmers are passionate about what we do. Our job can be pretty mentally taxing and, if you don't love it, it would be pretty rough to endure that day in and day out.

Because of that, we generally find that the programmers who survive the climb are passionate folks. Really think about that for a minute. I'll give some examples.

Kent Beck is a name I bet most of us know. One of his great successes was actually writing a book of guidelines for how individual lines of code should be structured. He had to care about the individual lines. That's how far he had to go to manage his programming. He's also done a ton for testing, for similar reasons.

1

Ruby is a melting pot language. It borrows ideas from many things that came before. It combines several different programming philosophies.

This aspect of the language can be a plus. It means that Ruby is suited to multiple applications. It also opens up some pragmatic shortcuts. Even better, it sometimes encourages us to think about problems using a different lens of thought.

Of course, this cuts both ways. Ruby living at the intersection of many ideas does have some downsides. First, there's more to learn than you find with some simpler languages. There's a cost for the extra knowledge we have to track. Even worse though, in my opinion, is that it's sometimes hard to know exactly what Ruby's style really is.

Going Off Script

One culture Ruby borrowed heavily from is that of the so called "Scripting Languages." The main source of these features was Perl, in my opinion, but you can also find influences from Bash and other sources. I found this comforting since I came to Ruby from Perl, but the truth is that it bothers some people.

22

That original description of the problem was for C# developers and it didn't have things us Rubyists love, like tests. Luckily for us though, Jim Weirich ported the code to Ruby and added the tests as he did so. Let's use that version of the challenge.

As always, I recommend you try the refactoring before you read what I have to say about it. Being familiar with the problem will help you understand what we are dealing with below.

Setup

The first step in this problem is to get the code running locally. I started by pulling down Jim's code from GitHub:

11

In the last article, I built a quick and dirty solution to PuzzleNode's Hitting Rock Bottom puzzle. I didn't use specs, objects, or even multiple files. In this article, I'll repeat the exercise but using all of those elements. Let's see how that affects the results.

The Disciplined Approach

This time, I'll reign in my desire to push forward and solve the problem more carefully, without having to build a fully formed algorithm all at once.

I still think the input is the right place to start. I need to read in the units. Let's add a spec for that in spec/parser_spec.rb:

require"minitest/autorun"require"stringio"require"hitting_rock_bottom/parser"describeHittingRockBottom::Parserdolet(:units){42}let(:io){StringIO.new(units.to_s)}let(:parser){HittingRockBottom::Parser.new(io)}it"reads the units number from the beginning of the stream"doparser.units.must_equal(units)endend

If you haven't seen the standard stringio library before, it simply wraps a String with an IO interface. That's will allow me to call gets() and other IO methods on it in my implementation. It's perfect for tests like this.

1

I want to take a look at some of the differences between Cowboy Coding and Test-Driven Development. To do that, let's solve a problem both ways and see what we can learn from the exercise.

A Puzzle

I needed some random problem to solve in this article and the PuzzleNode site is pretty handy for that. Two programmers I've been working with have recently experimented with problem number 11, Hitting Rock Bottom, so I am familiar with it. Let's use that.

You will probably want to read through the challenge before finishing this article. You may even want to try solving it yourself, just so you'll be more familiar with what I am doing here. The short, short story is that this problem is about simulating the flow of water into a cave for a fixed amount of time and then measuring the depth at each point. It doesn't take too long to solve.

Setup

Let's setup a project. I created a few directories and pulled down the data files given with the problem:

21

I love studying how the human brain works. It's an amazing biological machine capable of impressive feats. Of course, it also has its quirks.

For example, the brain is a terrific pattern matcher. That's probably one of its favorite activities. It wants to do this so much that it will often even find patterns that aren't there.

While this "feature" of your brain can get you into trouble, you can also make it work for you. Some parts of programming really are about patterns. If you prime your brain with the right data, it will just take over and do one of the things it does best.

How I Teach Iterators

One evening, at a post Ruby conference dinner, Glenn Vanderburg and I had a lengthy discussion about iterators and patterns. During this discussion we nailed down a plan for how the iterators could be taught.

I know that we've both used the strategy we came up with in multiple Ruby trainings and we have both seen it work wonders. This is hands down the best way to learn the iterators, in my opinion.

13

[Update: the Rubies in the Rough articles were originally available by subscription, but they are now free on this blog.]

I know this blog has been quiet for quite some time now, but I'm still writing about Ruby. In fact, I'm very excited about a new series of articles I am producing called Rubies in the Rough.

In that series, I am working hard not to teach the language or syntax or anything else that's trivial, but the thought processes behind how I program Ruby. I think this is some of the most important work I've ever done for Ruby. Anyone can learn a few keywords, but understanding how to think about problems and the solutions we code for them is key, in my opinion.

This new series isn't free, but it is a steal. It works out to two bucks an article and I often write close to 20 printed pages. This is some serious content.

If you want to learn how I think about programming, do yourself a favor and subscribe to Rubies in the Rough. It can teach you to see programming challenges as I see them and loan you my confidence as you solve them.

11

In the last article, I talked about the importance of a culture that encourages experimentation. It's hard to fiddle with something and not gain a better understanding of how it works. That knowledge is valuable to us programmers. I mentioned though that the way Perl programmers experiment is not the same way us Rubyists do it. Let me show you some actual Ruby experimentation I've witnessed over the years…

Executing Your Email

Some of Ruby's features are fairly obscure. Even worse, some of us who use those obscure features try to bend them to even stranger purposes. This is one way Rubyists like to experiment. Ironically, the features I'm going to talk about in this article are inherited from Perl.

Ruby can literally use your email as an executable program. Assume I have the following saved in a file called email.txt:

Dear Nuby:
I just thought you would like to know what the Hello World program looks
like in Ruby. Here's the code:
#!/usr/bin/env ruby -w
puts "Hello world!"
__END__
I hope the simplicity of that inspires you to learn more.
May Ruby Be With You,
Ruby Jedi

About

James Edward Gray II was a part the Ruby community before Rails
ever shipped. He wrote code and documentation that now come with
the language. He ran two Red Dirt Ruby Conferences and is was
a regular on the Ruby Rogues podcast for years. He now creates
videos showing real programming in action.
He does all of this just because he loves to program. This site is
where he writes about that.

Projects

Latest Tweets

Maybe if the number of people solving the #AdventOfCode keeps dropping, I have hopes of making the leaderboard around Christmas. #day9