Skip to main content

Posts

Getting started with Docker

Docker, in the beginning, can be overwhelming. Tutorials often focus on creating a complex interaction between Dockerfiles, docker-compose, entrypoint scripts and networking. It can take hours to bring up a simple Rails application in Docker and I found that put me off the first few times I tried to play with it. I think a rapid feedback loop is essential for playing with a piece of technology. If you've never used Docker before, then this is the perfect post for you. I'll start you off on your docker journey and with a few simple commands, you'll be in a Docker container, running ruby interactively. You'll need to install Docker. On a Mac, I prefer to install Docker Desktop through homebrew: brew cask install docker If you're running Linux or Windows, read the official docs for install instructions. On your Mac, you should now have a Docker icon in your menu bar. Click on it and make sure it says "Docker desktop is running". Now open a terminal and ty
Recent posts

Fun with Ruby - to_s

irb> 1 => 1 irb> 1.to_s => "1" irb> 1.to_s * 2 => "11" irb> "1" *2 => "11" irb> 1.to_s *2 => "1" # huh? #let's try that again with 2.... irb> 2.to_s => "2" irb> 2.to_s * 2 => "22" # good irb> "2" *2 => "22" irb> 2.to_s *2 => "10" # wtf? Sometimes a space makes all the difference. Ruby's #to_s on an Integer takes an optional parameter which specifies the base you are working in. In this case, passing in *2 has set our base to binary.

Development tips - don't use request as a variable name

Many web development frameworks like Ruby on Rails or Padrino or Sinatra make use of some magic variables. It's always a good idea to become at least partially familiar with these in case you fall into the trap of over-writing one of these, sort of akin to the way you can, in Javascript, carelessly declare a variable called "console" and completely destroy your ability to debug your javascript in chrome. This sort of problem can arise in Ruby on Rails when you are hacking away in a controller. Perhaps you've got a gem in your project that calls an api on the web - Let's call it the rubyflewtoo api that issues a random number when you call the "get" method. You might be tempted to write some code like this: request = rubyflewtoo.new @random_number = request.get bounds: {'lower':10, 'upper':20} And the gem magically gives you back a random number between your lower and upper bounds. The code makes sense and the variables look reasona

Context, Tooling and the Beginner Programmer

Renée De Voursney talking at the AU Ruby Conf about the trials and tribulations of learning Ruby. Renée De Voursney - Teaching Ruby for fun and profit from Ruby Australia on Vimeo . She talks about context and how there are so many disparate moving parts to get to grips with before one can "become" part of the Ruby community. Gaining a basic understanding of all the moving parts that encompass not only the Ruby language itself, but the social norms of RSpec, Git, Gems, Github, RVM, *nix, Macs, TDD and various command line tools, is too much of a hurdle for many people to jump. The biggest problem with a complete novice trying to get into programming is always some sort of feedback loop that will give them the justification to carry on. I'm a great believer in learning by debugging, but at the same time, giving the novice quick wins is important. Get them up and running quickly from nothing (and I mean nothing - no tools installed on their machine yet) to "he

Poor person's guide to managing Ruby versions

Understanding the guts of Ruby Version Management by rolling your own I've been tinkering with a fresh install of Ubuntu 12.10, setting up a nice clean development environment. One of the first things to do, of course, is implement some sort of Ruby version management. RVM and rbenv seem to be the clear winners in this arena, though there are a lot of tools out there that do a similar job . Writing your own version management for your Rubies isn't actually all that difficult. At it's core, we need need two things: A way to segregate the executables of the various versions A way to call the versions at will Segregating versions is trivial - working with files and folders, we can put the various versions into named directories. Actually executing our different versions is not all that difficult either. One way would be to create aliases with version numbers and explicitly call those when we want to use them. The more popular way, however, is to manipulate our PATH

Rails 3.2, MiniTest Spec and Capybara

What do you do when you love your spec testing with Capybara but you want to veer off the beaten path of Rspec and forge ahead into MiniTest waters? Follow along, and you'll have not one, but two working solutions. The setup Quickly now, let's throw together an app to test this out. I'm on rails 3.2.9. $ rails new minicap Edit the Gemfile to include a test and development block group :development, :test do gem 'capybara' gem 'database_cleaner' end Note the inclusion of database_cleaner as per the capybara documentation And bundle: $ bundle We will, of course, need something to test against, so for the sake of it, lets throw together a scaffold, migrate our database and prepare our test database all in one big lump. If you are unclear on any of this, go read the guides . $ rails g scaffold Book name:string author:string $ rake db:migrate $ rake db:test:prepare Make it minitest To make rails use minitest , we simply add a require

Ruby, facemash and the Elo rating system via BDD

Reproducing the Elo rating algorithm in Ruby is a little challenge that I took upon myself recently for a small hack. The same Elo rating system that was scrawled upon the glass in "The Social Network" as the algorithm that Mark Zuckerberg used on rank people on FaceMash. As an exercise, here's a pass at it with a little BDD thrown in for good measure. A new file - elo.rb: require 'minitest/spec' require 'minitest/autorun' describe "result" do describe "when both players start with rating of 100 and k factor of 30" do it "must return 115 for the winner" do new_rating.must_equal 115 end end end Starting a new algorithm is always tricky. We know that the Elo rating system is essentially concerned with assigning ratings to players based on the outcome of their games or matches. In fact, it is widely used as a chess ranking algorithm. At a first glance then, I thought I might want to jump in and start mo