Tuesday 11 February 2014

Frickin Chocobos - Tennis elbow generators

Ok. We have a Hexxit server.
So one day my other half suggests I breed chocobos.
"Why yes," I say, "That sounds like a good idea."

WTF was I thinking?

I managed to scrounge up a few yellow choco-bastards and corralled them as best I could.
Grew acres and acres of frickin Gysahl Greens.
Then off to the pens.
Right-Click, Right-Click, frickin-right, frickin-click.

I am so SICK to the back teeth of right clicking.
And the incessant "Wiperoo" sound they make.
Aaaaaaah. Frickin Aaaaaaaah!
I'm going nutso-bong!

So after many, many hours of right clicking I get a few Greens and Blues.
And after many, many hours of chasing the mad, flighty bird-bugger-it-i'll-kill-them-alls around I manage to get 3 whites ones.

And... Wait for it... They're all FUCKING female.

Now I want to share something.
Something dark.
Very dark.
And I *KNOW* dark.
I want to imagine you've been breeding these frickin noisy flighty birds for ages.
And you try once again to breed a white one.
And you get yet another frickin baby Blue or Green.
And you snap.
Snap.
Snap deeply and profoundly.
"Crack goes the mind boys, crack, crack, crack."
And you chase that frickin little choco-kid-bastard around with a steel sword until you get it.
And you retreat from the awful, hateful, awful, incessant "Wiperoo" sounds to the safety of your house.
Burdened down by frickin blocks of frickin feathers.
WHAT THE FRICKIN USE ARE THE FRICKIN FEATHERS?
I HAVE FRICKIN CHESTS FULL OF THE STUPID THINGS.

Anyway, calming down now.
Deep breaths.
Chests with 12 by 64 blocks of choco-drum-sticks.
Cooked.
Cooked child chocobo-drum-sticks.
Nom.
Nom Nom.
Nom-frickin-child-chocobo-drum-sticks.

Oh the choco-bird-ity of it all.

Then!
Then!
Then your spouse announces that he's been to the Nether and managed to coax three PURPLE chochobos back.
And crows over his achievement!
Oh.
And he has equipped them with saddles.
And...
And...
I just remember being up to my arm pits in chocobo-chicks with a steel sword.

I FRICKIN HATE chocobos.

And I have tennis elbow.
In the real world.
Frack it.

I'm off to dig a hole.

Update! My other half took some umbrage at the above. He suggested that his effort to bring back Purple ones from the nether was a Herculean task and he should be lauded. I did that female thing of staring at him in a 'pointed', 'dagger' like manner. He suggested that I open a Kentucky Fried Chocobo Stand. I demurred. Meh.

Ruby: Class loading/unloading example

Recently I had the unfortunate task of writing specs to test a singleton.
Why would that be a chore I hear you ask?
Because the singleton in question had several configuration options.
Most of which were file based.
So out of the ~60 specs, 50 of them were for error conditions.
(File missing, folder not readable, not JSON, JSON but with invalid stanzas etc)

The problem was that each test had to start with a clean slate.
The singleton class must be pristine and new each run.
How the hell do you do that?

Unload the class, then reload it.

I fiddled about quite a bit with this, so I've put together a simple project that illustrates how this is done.

The bottom line is that you have to send :remove_const and the class name to the module or whatever your class is defined in.

For example, say a class is defined in a file skavee_bloopsie.rb thus:

module Skavee
  class Bloopsie
  end
end

Then this is what you run to unload and reload it:

Skavee.send(:remove_const, :Bloopsie)

This works at any depth of modules:

module A
  module B
    module C
      class Z
        def ohai
          'ohai!'
        end
      end
    end
  end
end

A::B::C.send(:remove_const, :Z)

You can test this yourself by copying and pasting this into irb:

module A
  module B
    module C
      class Z
        def ohai
          'ohai!'
        end
      end
    end
  end
end

a = A::B::C::Z.new
 => #<A::B::C::Z:0x007fc78c9b1dc8>

puts a.ohai
 => "ohai!"

A::B::C.send(:remove_const, :Z)
 => A::B::C::Z

b = A::B::C::Z.new
 => NameError: uninitialized constant A::B::C::Z

And here's the Gotcha!

a.ohai
 => "ohai!"

Wha? Well it's obvious. The instance is still being referenced.

So be careful out there people.

An example gem is available via my repo at https://github.com/ZenGirl/ruby-class-unloading