Hardscrabble 🍫

By Max Jacobson

making a pull request to rails

14 Jul 2015

Today I impulsively made a pull request to Rails, which feels kind of like a milestone for me. It’s about two years since I started using Rails at the Flatiron School. It’s also been about two years since the method I edited was last edited. I feel like there may be a reason and it won’t get merged, but who knows? I feel sort of exposed.

This post was helpful for me: Eileen Codes | Getting Your Local Environment Setup to Contribute to Rails.

In order to make a proper contribution, I needed to know that my change didn’t break the existing tests, and so I needed to be able to run the tests.

I also wanted to be able to add tests and confirm that they pass. So I really needed to be able to run the tests.

I had some trouble configuring my local environment, despite the post explaining it well (…databases…), BUT the post mentions rails/rails-dev-box which lets you skip a lot of the environment configuration by using a preconfigured virtual machine, and that turned out to be a god send of a casual aside for me and I’m writing this post largely to promote the existence of that project because it’s awesome.

It uses vagrant which is kind of magical… I had never used it before and it totally blew my mind. It allowed me to have a tmux session with windows like I’m used to, with the code open in vim in one tab using all the existing configuration from my local Mac machine, and then another tab where my code changes were immediately available for running the tests against in the Linux virtual machine. It was super seamless and sweet.

Here’s a four minute long gif of what it looks like – I’m refraining from embedding it so you don’t need to download a 4mb image if you don’t want to, and so you can open it in a new tab to start at the beginning easily if you want to.

I don’t really love my solution and I should probably consider it further, but I know the tests are passing, including the new one I added. I think being a little sleep deprived lowered my inhibition tonight.

Quick tip: track command history in zsh

11 Jul 2015

I switched from bash to zsh a few months ago and it’s been mostly sweet. I noticed that it wasn’t tracking my command history, so I did a little googling and got it working by adding these commands to my ~/.zshrc:

export HISTFILE=~/.zsh_history
export SAVEHIST=1000

I picked 1000 kind of randomly; a bigger number would probably be fine or nice.

using method_missing with class methods in Ruby

08 Jul 2015

Ruby’s method_missing let’s you write some weird code:

class Poet
  def initialize
    @words = []
  end

  def method_missing(message, *args, &block)
    @words << message
    message.to_s.end_with?("?") ? sentence : self
  end

  private

  def sentence
    @words.join(" ")
  end
end

puts Poet.new.why.not.go.for.a.walk?

I’ve seen method_missing used to handle unexpected message on instances of a class before, but never for class methods. But, like, why not?

class LoudSpeaker
  def self.method_missing(message, *args, &block)
    if [:exclaim, :yodel, :howl, :sob, :beg].include?(message)
      puts args.first
    else
      # we don't want to handle this missing method, we want Ruby to raise the
      # NoMethodError it ought to
      super
    end
  end
end

LoudSpeaker.exclaim "helloooo!"

It’s just methods, so go for it.