Ruby Slop Example

25 Sep 2014

One of my favorite features of slop is the automatic help generation. But it’s not intuitive. It doesn’t print out the help when the parsing fails. This isn’t very unix-y. So every time I want to use slop, I have to look up this snippet I saved for myself. So I’m posting it here. This is the only slop example you’ll ever need.

Unix style CLI program in Ruby

require 'slop'

opts = Slop.new(strict: true, help: true) do
  banner 'Usage: slop_test.rb [options]'

  on 'resume=', 'Your resume file', required: true
  on :s, :skill=, 'Skill Name', as: Array, arguments: :optional
  on 'v', 'verbose', 'Enable verbose mode'  # same as adding required: false
end

begin
  opts.parse

  # validation passed
  puts "Here's the data"
  puts opts.to_hash
rescue Slop::Error => e
  puts e.message
  puts opts
end

Calling it like this will fail:

$ ruby ./slop_test.rb
Missing required option(s): resume
Usage: slop_test.rb [options]
        --resume       Your resume file
    -s, --skill        Skill Name
    -v, --verbose      Enable verbose mode
    -h, --help         Display this help message.

Great! Then using it correctly will do this:

$ ruby slop_test.rb --resume hechicero-del-relámpago.doc
Here's the data
{:resume=>"hechicero-del-relámpago.doc", :skill=>nil, :verbose=>nil, :help=>nil}

The resume flag is the only required one, so in this case that’s how it’s run correctly.

Update: I was very happy that @lee_jarvis (the slop author) accepted my pull request to put this example into the README.

Redis and 595 Timer States

24 Sep 2014

I wanted to learn about how a 595 timer chip works. I’m a code dude. So when I see this integrated chip and all its pins, it’s scary. I’m sure an EE major is giggling right now but that’s just Impostor Syndrome. For no reason beyond this, I wanted to visualize and grok a 595 timer’s state at any given point by having it’s pins mapped to Redis key/value pairs.

Here’s a quick video explanation of the project.

Continue Reading →

Positive Change

17 Aug 2014

I’ve been in Portland for a week. So far, it’s amazing. I really don’t want to blather on about how great it is because, to be honest, I’m afraid of boyish optimism. This town, like college, will probably give back whatever I put into it. So I’m pacing myself. I think it will be good though.

Our house is completely empty while we wait for our movers to arrive and that’s ok. I’ve been getting a lot done without all the distractions. One of my favorite pictures of Steve Jobs is where he is sitting in an almost empty room with nothing but books. I’m not trying to be Steve Jobs but I appreciate the minimalism because my house looks very similar to this picture right now.

Continue Reading →

Default DC Tech is Just Bad

06 Jun 2014

The opinions of this blog, but especially this post are mine and not my employers’.

I’m done with DC. I need to archive the reasons why for myself. I hope this serves as a free field trip to the DC area for anyone outside the beltway.

TL;DR

If you move to DC for the tech jobs, you are going to have to prune a lot of C-minus government work if you are good. All the while, you will be paying for local benefits you are not taking advantage of. This is the land of politics, military, intelligence, big government and lobbyists. I tried to influence from within but now it's time for me to GTFO and move to Portland to try to find "actual reality" jobs.

Continue Reading →

Passion

19 May 2014

On a Ruby Rogues podcast about Passion, Avdi continued to enlighten and entertain me with his insights. I’ve really been enjoying his speaking style and voice lately through tapas and talks. If he reads this, I hope he understands I don’t disagree with what he is saying; I thought he would enjoy a related story.

Honestly, this topic is so massive I don’t think I can really offer too much more than the Rogues did on the podcast so I encourage you to listen to the episode yourself. It has almost nothing to do with programming or Ruby. I feel that philosophies and stories about passion are so close to the difficult and inevitable goal of “master yourself”, which is both complicated and personal, I can just barely approach the topic and then a rat’s nest of anecdotes and advice explodes all around us.

With that context laid out, here are a few stories.

Continue Reading →

DRY up Methods with Ruby Blocks

14 Sep 2013

Let’s do something terrible by hand. First, here’s our data. It comes from a database.

db_results = [
  { id: 1, login: 'mjay', roles: ['user'], projects: ['muffins'] },
  { id: 2, login: 'rroke', roles: ['admin', 'user'], projects: ['security'] },
  { id: 3, login: 'tpain', roles: ['user'], projects: ['muffins'] },
  { id: 4, login: 'ghaz', roles: ['admin', 'user'], projects: ['muffins', 'cakes'] },
  { id: 5, login: 'bbarker', roles: ['user'], projects: ['pies'] }
]

Now when working with these people, we probably could get away with doing something like this for a while:

# find all admins
admins = db_results.select {|user| user[:roles].include? 'admin' }

Which is fine. Until you want to find out what people are on the Muffin Project:

# find all people working on the muffins project
people_on_muffins = db_results.select {|user| user[:projects].include? 'muffins' }

But as you keep working, you might be getting a feeling of deja-vu. The two methods above are very similar. You might be inspired by other Ruby libraries which give you a tiny DSL or at least allow you to pass blocks into methods to be more expressive.

Continue Reading →

Problems with "The Cloud"

27 Aug 2013

I've been thinking about the problems with The Cloud outside it being a raging buzzword. It really comes down to Control and Connectivity. That's the problem but allow me to elaborate.

Control

Google Wave is a great example of control loss. If you really put a lot of energy, stock and trust into Google Wave as a content store for your team, brain or idea then you might feel deflated by its cancellation. Even as an idea and a disruptive alternative to E-mail or SMTP crappiness, it's a shame it had to die. So what now? Wait for an open source version? Host your own?

The idea was to "put it in the cloud" and forget about it. But when the cloud changes outside your control, you have to be aware of it again. Now you really have to think about the cloud itself. It's not such a vague black box which is what the cloud diagram really means.

Another example of control is YouTube. I use YouTube favorites as a persistent list. I see a cool video, I favorite it and I feel like I sort of own it, or at least it's in a list that I can refer to later. But take a look at this:

youtube_whoops

What were those things? Who knows! Now, I have to think about "the cloud" again. These are temporary videos that someone else ultimately controls. I'm just adding references to a list. I don't own the clips. They are transient. They are ephemeral. I'm out of control again. I don't even know what media I've lost. Do I mitigate again? Do I suck down a list periodically and do a diff?

Connectivity

I recently got a Roku box for my TV. It's a great box. During registration it does a bunch of sign up and account creation. But it doesn't work without uPNP enabled on the router. This isn't even a connectivity outage thing, it's a connectivity assumption that I have a certain kind of firewall that can't have holes punched in it ... or that I'm not capable of punching the holes myself. I don't even really know why Roku does this uPNP thing. All I know is, it wouldn't even finish the setup until I made this change. Now here's a device that doesn't work without connectivity or a clear path to connectivity.

Think about how picky that is for a second. If it's not picky then think about how many technical barriers there are to pure or uniform Internet. Everyone brings their own quilted environment and it's a mess.

IPv6 Spike

28 Jul 2013

A spike is when you play around with something and then throw it away for the purposes of learning. So, let’s play around with IPv6. I had read a little bit about it but essentially my working experience with IPv6 was nothing except for disabling it. Let’s learn some stuff!

I'm going to skip over all the history of IPv6 and assume that you agree with me and think that this is important and relevant to the future of the Internet.

Setup

First, build 4 Ubuntu VMs. I'm using 13.04 but any current Linux distro should work, just the packages and paths will change. I found the best way is to build a simple VM and then clone it 3 more times (in Fusion this is copy/paste and resetting the MAC address). You'll need four machines to simulate a local network. You won't need any network hardware and VMware will be able to simulate everything we need. You can actually do this whole experiment on one real box (cool stuff)!

The goal of this spike is:

  • Build 4 VMs
  • Make a router, a web server, a dns server and a client
  • Hit a web page between two network boundaries over IPv6 only

Continue Reading →

The Best Way to Read CSV in Ruby

16 Jun 2013

CSV is awful. CSV isn't well formed. It isn't hard to use because it's bloated and slow. CSV is hard to use because it's just a dumb data format. However, sometimes all you have is stupid data and who cares, let's do this thing and blot out the memories.

I assume you know how to use the CSV module that's built into Ruby. It's pretty easy. You just read a file in and you get some 2D array back. It usually comes out pretty horrible with long methods and little room for nice abstractions.

So what if you want to polish it up a little bit? Maybe you aren't just going to kludge this thing again and hate yourself later? What if you aren't just going to load this into a database? What if you want to do some quick CSV analysis but at the same time make it come out sort of readable?

Let's take a look at an abstraction layer and see how we could write a CSV loader for a guest list. We're going to have a dinner party and evite gave us a crappy CSV dump of who's responded so far. Well, it's what we have. But how many people are coming and how many groups aren't allergic to peanuts? We want to know how many peanut M&Ms to buy.

Here's our data:

Name, Plus, RSVP'd, Peanut Allergies
Tom DeLuise, 1, No, Yes
Mel Brooks, 3, Yes, Yes
Lewis Black, 5, Yes, No
Jon Stewart, 3, Yes, Yes
Jim Gaffigan, 0, Yes, No

Continue Reading →

Rails Dev Shops in Washington DC

10 Jun 2013

What shops, companies, consultants, startups or other folks are using Ruby or Rails (on any level)? Contact me on twitter if you want to be added or you have corrections: @squarism or leave a comment below.

Continue Reading →

The Supermodel Ruby Gem Loses Data

03 Jun 2013

Actually no.

I love Supermodel. It might be overcome by ActiveModel::Model in Rails 4 but until then Supermodel is a fantastic in-memory database for Ruby that has a lot of advantages over using just a plain hash or trying to roll your own.

However using it with a large amount of data, we noticed it loses data. Sometimes, a few records. Other times, a few more. It was really random. We were confused. Looking at the docs, this is the default class maccman has in his README.

class Test < SuperModel::Base
end

That works no problem. We looked at the IDs that it uses and saw that it's using the Ruby ObjectID which is about 14 digits long.

#<Test:0x007f80e41dbd18 @new_record=false, @attributes={"bacon"=>"tasty",
 "id"=>70095779847820}, @changed_attributes={}, @validation_context=nil,
@errors={}, @previously_changed={"bacon"=>[nil, "tasty"]}>

Continue Reading →

Super Interesting Talks from RubyConf 2012

29 May 2013

Trying to summarize someone's 30-60minute talk is really hard. So apologies go out to anyone I'm trying to paraphrase here. I took it upon myself to watch every single video from RubyConf 2012 which started airing in November. It's May now. There's a lot of content there and you can't just slurp it down and expect to process it all. So I thought I'd leave little breadcrumbs to myself noting which things were super interesting to me.

Real Time Salami - Aaron Patterson
Any presentation by @tenderlove is great and this one was fun and interesting as expected. Aaron talks about parallelism, streaming and making Salami (actual salami).

Continue Reading →

Using a Redis as a Database

04 May 2013

The Spike

I was spiking on Redis recently. I wanted to use the redis-objects gem to simulate a shopping cart app even though the README specifically says

Just use MySQL, k?

I wanted to see what would happen if I tried it anyway. So the README and examples for the redis-objects gem are great so I'm not going to rehash what's there. However, I will say though that the example has you hardcode the id field to 1. That detail snuck up on me.

Continue Reading →

The Blub Paradox and Delicious Pie

21 Apr 2013

Anything worth doing is worth doing well. “That’s Good Enough” isn’t good enough. There are some cases where you are writing a one-off script but those tasks are few and far between. Most people don’t get paid to write throw-away code. Most developers aren’t sysadmins writing procedural code on the level and complexity of Bash. Even if you are, is this the only style you know? Do you shy away from more complex tasks? Can you not break down an any size problem into smaller pieces?

A musician practices their art constantly. An athlete trains. And yet, we in the tech community have to go out of our way to find people who work on personal projects on the side or have the will/motivation to learn new things outside of “the job”. If you don’t learn on your own then you have to figure it out as you go. And you are already very busy so I know you’re just skimming over this post anyway. So let’s break this down in a bulleted list.

  • Developers work best when they’re challenged but not overwhelmed. Optimal work is a run, not a jog or a sprint.
  • Developers will step-up or fill-in a task to make it challenging as stated above.
  • Your classic boss doesn’t really care how you solve problems.
  • Culture takes 20 years to change.
  • The Blub Paradox says (among other things) that you can’t make anyone understand the power of a different language because developers become sedentary.
  • Good consultants are already busy so no one is going to save you.
  • Training or bootcamps won’t change your skillset or habits by a large percentage.

Continue Reading →

Ruby p385 benchmarks

16 Feb 2013

I was playing around with the falcon p385 patch to see if it's any faster than some of the more recent MRI rubies. TL;DR version: looks like p192 is faster than p385 of any type or tweak.

Here's how to get a p385 Ruby version patched with funny falcon's performance patches using RVM.

mkdir ~/.rvm/patches/ruby/1.9.3/p385
curl https://github.com/funny-falcon/ruby/compare/p385...p385_falcon.diff > \
$rvm_path/patches/ruby/1.9.3/p385/falcon.patch
rvm install 1.9.3-p385 -n perf --patch falcon
Then rvm use 1.9.3-p385-perf or set it as your global ruby.

Test Setup

The following benchmarks were run on an i7 server with a RAID5 array. The disk is slow (lack of large cache) but the benchmarks were run on the same box so it should compare apples-to-apples.

Continue Reading →

Hi, I am
Chris Dillon