From: Gregory Millam Date: 2004-04-03T09:51:44+09:00 Subject: Re: Learning Ruby, was a C geek... Received: Sat, 3 Apr 2004 08:37:54 +0900 And lo, Nicholas wrote: > I've written it, and it works correctly when compared to my program > written in C, but at its core is a large case-when-end statement. > This is not `sexy.' I was wondering if there was a better way to do > this in Ruby, or if I'm just missing the point. This is a project similar in scope to an NES emulator I've written in C. When doing so, I tried and benchmarked 2 situations: switch (opcode) { case 0x01: case 0x02 ... (etc, for every opcode) } And: typedef void (*operand)(address); operand opcodes[0x100]; (You'd use 0x10) op_0x01(int address) { ... } opcodes[0x01] = op_0x01. Then call using: opcodes[0x01](addr); Ruby caters to the second style rather well, adding in lambda blocks. ops = [] ops[0x01] = lambda { /* incrememnt accum 1 */ |address| Cpu.accum += 1 } Either a hash or an array could do the job. You'd really just need to fetch op[instruction byte 1 >> 4], if I'm reading that specs document correctly. Hope that helps - Greg Millam