Jay McGavren's Journal

How a Head First author spends his days off

View on GitHub
2008-06-04

Naughty, but I like it...

Normally you can’t add objects to an array like this:

irb(main):001:0> [1] + 1
TypeError: can't convert Fixnum into Array
        from (irb):1:in `+'
        from (irb):1

But Array#+ tries to call to_ary() on its target if it’s not already an array. So just add that method, and voila:

irb(main):002:0> class Fixnum; def to_ary; [self]; end; end
=> nil
irb(main):003:0> [1] + 1
=> [1, 1]

Wait, why stop there?

irb(main):005:0> class Object; def to_ary; [self]; end; end
=> nil
irb(main):007:0> [1] + "foobar" + File.new('test.xml')
=> [1, "foobar", #<File:test.xml>]

Of course, you should probably be using Array#<< for this. But I wonder what other applications there are…

Read more...
2008-06-04

A time-sink is born...

Found an old e-mail to my brother that is basically the inception of Zyps. Not sure of the exact date, because dates got eaten by a corrupted mbox file eons ago. Whenever Wired profiled the MIT Ants project, I guess.

Read more...
2008-06-04

__pre_to_s, __post_to_s...

The design by contract bits in the Ruby Cookbook are interesting if only for the fact that they got me thinking about all the options for wrapping a method. By including a module in your class, you can set up code to run before or after a method call, for a specific method or all methods. That means you can check arguments, check output, or maybe even alter the input or output.

When flipping through the Module documentation this time, my only thought was, “Oh, neat, they have #included, #extended, and #method_added callbacks. Those might be useful someday.” Well, only now am I starting to see concrete uses, and they’re potentially so powerful it blows my mind.

My main remaining gripe is how sloppy aliasing methods is. I’d rather not have a bunch of __pre_x(), __post_x(), __original_x() methods lurking under the hood of my classes. But at least it’s becoming clear that I can hide this ugliness from myself.

Read more...
2008-06-04

Have you noticed what a central notion powerup management is to many games? D and I were playing Gauntlet last night, and we knew we’d barely squeak by the level we were on. So every hunk of cheese that dropped was carefully rationed to ensure it wasn’t wasted on a mostly-full health bar. This happened on the fly, but required a bit of hasty debate.

I don’t think this is even an intentional gameplay mechanic for most games that have it, but maximum HP definitely increases the need for teamwork. I wonder if the model can even be improved upon…

Read more...
2008-06-03

Oh. My. God.

Just spent half the afternoon trying to update a field via this legacy app I maintain, only to find that it reads the value from an update file and then silently discards it. (And that’s what it’s “supposed” to do.)

And then while trying to explain my own dumbassery, my co-worker realizes that the field she told me needed fixing wasn’t even the right one. Arrrggghh!

Read more...
2008-06-03

I’ve heard from a couple places now that smaller doses of caffeine throughout the day are better than a single jolt in the morning. So I’m trying to switch to green tea, which is supposed to have less…

But it seems like four times the kick, and not in a good way. I feel like my heart is going to pump its way right out of my ribcage. And it’s definitely due to the tea - this is the second occasion it’s happened.

Diana had similar effects several years ago. What the heck is in this stuff?

Read more...
2008-06-02

Looks like my company has purchased a license for a commercial wiki to use internally. Finally, a means of documenting database tables and applications that isn’t too painful to actually use… I’d been generating HTML from POD, and that was my unique solution - everyone else had their own.

Read more...
2008-06-02

Local aspects of modules?

Everyone’s so up in arms about re-opening core classes like Kernel and String in Ruby… Why not just apply changes to a target module only when viewed from within the current module or class?

class Bar
	def baz(fizz)
		fizz * 2
	end
end
module Foo
	local class Bar #Or some other keyword.
		def baz(fizz)
			fizz * 5
		end
	end
	def buzz
		puts Bar.new.baz('a') #'aaaaa'
	end
end
puts Bar.new.baz('a') #'aa'

That way you could tweak behavior to your heart’s content without worrying about the expectations of the other libraries you were using. This seems so obvious I’d be surprised if I was the first to think of it, so someone please let me know what problems have been encountered.

Read more...
2008-06-01

Move your ass.

If you are reading this, odds are…

  • You are one of the richest 5% of Earth’s population.
  • You live in the most peaceful era in human history.
  • You are at least reasonably healthy, both mentally and physically.
  • You have more educational materials, literature, music, and movies available to you, several times faster and cheaper than your parents did.

All of the above are threatened on multiple fronts.

You need to realize immediately how unbelievably fucking lucky you are. And that your luck may not last forever.

Then you need to go out and do something for humanity with the incredible resources at your disposal.

Do NOT waste one more day. You don’t know how many you have left.

Read more...
2008-05-31

Just finished up my presentation for Zyps...

I really can’t pronounce it a huge success; I had a group of about ten people. Not insanely pleased with how everything went, but that may be just my own overly self critical eye on it.

Part of the problem was that I was flustered - I couldn’t get the projector working despite arriving 15 minutes early… 10 minutes in, Lorin Thwaits finally got it going with the help of some UAT staff. (Many thanks to him for rescuing the presentation!)

Got a couple questions, got modest replies, I think it was just too early in the morning for me and for everyone else.

Read more...
Copyright © Jay McGavren.