Skip to main content

Posts

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 "hello world&qu…
Recent posts

Poor man's guide to managing Ruby versions

Understanding the guts of Ruby Version Management by rolling your ownI'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 versionsA way to call the versions at willSegregating 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 variable. The PAT…

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 setupQuickly 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 documentationAnd 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, goread the guides. $ rails g scaffold Book name:string author:string $ rake db:migrate $ rake db:test:prepare Make it minitestTo make rails use minitest, we simply add a require statement to our test_helper.rb:…

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 modellin…

Stop asking for a ninja when you actually want a samurai

From the various appropriate entries on Wikipedia about ninja and samurai (emphasis mine)A ninja or shinobi was a covert agent or mercenary. The functions of the ninja included espionage, sabotage, infiltration, and assassination The shinobi proper, a specially trained group of spies and mercenaries Samurai were the military nobility Samurai felt that the path of the warrior was one of honor, emphasizing duty to one's master, and loyalty unto deathNow which one do you really want working for you?

Overriding equality and Test Driven Development

Ruby has, at its root, an Object. Methods available in Object are available to every class because every class in Ruby inherits from Object somewhere in its own class hierarchy. Of course, you can override methods in subclasses, changing the functionality of a root method.You might stumble on to this idea if you work through Test Driven Development By Example by Kent Beck, translating the Java code into Ruby as you go. At some point pretty early on, he overrides the equality method on the Currency class to better test if two instances are equal. I'm going to do the same here, working with Instruments instead of Currency.EqualityEquality in Ruby can be expressed using any of the following three methods object == other equal?(other) eql?(other) These methods are defined on the base Object. The default implementation of equality will only return true if both objects are exactly the same. The interesting thing is that although these three methods start out functioning the same, the do…

Rails Callbacks and Boolean Values

Rails has a lovely feature as part of ActiveRecord - Callbacks; to wit:Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after an alteration of the object stateBasically, that means that you can instruct Rails to execute your own code throughout the process of creating, updating and destroying objects/records. You can, for example, use them to set some default values on create, or perhaps do some last minute validation, or even fire an event just after you've saved a record. The available callbacks are:after_initializeafter_findafter_touchbefore_validationafter_validationbefore_savearound_saveafter_savebefore_createaround_createafter_createbefore_updatearound_updateafter_update:before_destroyaround_destroyafter_destroyafter_commitafter_rollbackAt the same time, Ruby methods have the concept of implicit return values. That is to say, any method will return the value of the last evaluated statement. This is neat - you don…