Ruby for Canadians: an instruction manual

One of the hardships about being Canadian is that most programming languages are quite simply, rude. Descriptions like ‘imperative’ and ‘declarative’ are enough to fill even the most impolite of Canadians with a vague sense of discomfort. Fear no more! Ruby is the sort of language that addresses all these concerns, and adds a familiar, maple-syrupy feel to your code. Here are the codes, for your perusal.

There’s no such thing as ‘too polite’

Ruby lets you open your classes and teach your fellow programmers some basic manners:

class Object
  def please
    self
  end
  alias_method :eh, :please
  alias_method :eh?, :please
  alias_method :pardon, :please
  alias_method :pardon?, :please
end
module Kernel
  alias_method :sane_puts, :puts
  alias_method :giver, :puts
  def puts(s)
      sane_puts "If you don't mind, " + s
    end
end

Now you can write correct and civilized code like you’ve always wanted:

giver "maple syrup".please.reverse.eh?
# If you don't mind, purys elpam

Don’t forget about Quebec

Ruby is the first language to pioneer French as the one true language. We have translated all the methods in Array and stored them in a dictionary called translations, that contains entries of the form :english_foo => :french_foo. A small iteration,

class Array
  Array.instance_methods(false).each do |f|
    if translations.key?(f)
      alias_method translations[f], f
    end
  end
end

And we can write glorious code that would make Quebec proud

def articles_aleatoires(matrice)
  matrice.echantillon(1 + rand(matrice.compte))
end
articles_aleatoires [1,2,3,4,5]
# => [1, 2]

Canadian slang

Fans of Bob and Doug McKenzie can breathe a sigh of relief to know that Ruby speaks the language of the Great White North, eh?

class Exception
  alias :sane_to_s :to_s
  def to_s
    sane_to_s + ". Take off, hoser."
  end
end
# >> raise ArgumentError
# ArgumentError: uncaught throw ArgumentError. Take off, hoser.

Digression for non-Canadians

Of course, you can use this idea of open classes for truly magical features. Good code is nothing if not mildly interesting.

class Fixnum
  alias :sane_equals :==
  def ==(x)
    not self.sane_equals(x)
  end
end
# >> 4 == 4
# => false
# >> 4 == 5
# => true

Unicode

This is where this post starts being less Canadian and more flat out crazy.

Ruby is down with Unicode in identifiers. That’s because Ruby is down with pretty much everything. The only thing I have yet to convince Ruby to do is to let me alias keywords, and I’m pretty sure that’s just because I haven’t tried hard enough.

If you add a comment to gently nudge at an encoding, you too can write this production ready code:

# encoding: utf-8
class Object
  def 
    sane_puts "BOOM"
  end
end

Guess what calling ☢ will do. GUESS.

Unicode can fill your boring, profesh code with whimsy:

if  == 
    puts 
end

Our advanced readers can also define a function named U+00A0, the non-breaking space, as seen here.

Bare words

We all know that Ruby has bare words from Gary Bernhardt’s Wat talk, but did you know Ruby also allows you to have bare words as function names? Because that’s totally a thing you’d want. Thanks to Richo Healey for the example:

self.instance_exec do
def method_missing(sym, *args)
  # Splat args if passed in from a parent call
  if args.length == 1 && args[0].is_a?(Array) && args[0][0].class == NameError
    args = args[0]
  end
  method_names, arguments = args.partition { |a| a.class == NameError }
  method([sym.to_s, *method_names.map(&:name)].join(" ")).call(*arguments)
rescue NameError => e
  return [e, *arguments]
end
end

Defining such a function doesn’t even look that improper, which is why I recommend moving the above code into a separate .rb file, deep at the bottom of a folder barrel.

self.class.send(:define_method, :"take off") do
  puts "♫ ♪ Coo loo coo coo, coo coo coo coo ♬ ♪"
end

This leads to the most excellent of results, and a job well done:

take off
# >> If you don't mind, ♫ ♪ Coo loo coo coo, coo coo coo coo ♬ ♪
# => nil

Nik Markwell has a neat implementation of a saner, more constrained version of this, which ends up looking like

as long as -> { i < 10 }, -> { puts i += 1 }

However, being practical and sane isn’t the Canadian way. If it were, most Canadians wouldn’t live in a place where 11 months of the year the air hurts your face. We don’t stand for useful applications of bare functions, and nor should you.

Next on our agenda

Convincing the W3C that the California Style Sheets spelling of ‘colour’ and ‘grey’ is the only appropriate one. Do not lose faith, Canadians. Now that the rest of the world has accepted curling as a sport, they’re ready to accept anything.

« Cat-DNS: a DNS server that resolves everything to cats When pull requests get tricky »