Silly Programming Mistakes Episode 1

Well, I feel pretty dumb at the moment. I just spent the last 6 hours trying to figure out why I couldn’t seem to use a Ruby Hash correctly and I just could not make it work. I’ve been using ruby for over a year now, and I’ve written plenty of code using hashes, so why was this so hard?

First, let me show you what I was doing. I’m writing a data persistence layer over the Google App Engine Java persistence store so that I can write my JRuby on Rails apps very similar to how you do using ActiveRecord. So I have a model objects and methods for defining properties and relationships on the class. Java is statically typed, so I wanted to ensure the translation from ruby objects to java objects happens correctly. To do this I have mapped the subset of GAE supported java types to a set of ruby symbols. To set up a model object you do the following:

class MyModel < DataAE::Entity
  property :id, :key
  property :name, :string
  property :age, :integer
end

And the DataAE::Entity class has the property function, like so:

@@types = Hash.new


def self.property(name, type)
  ...
  @@types[name] = type
end

Pretty straight forward, right? I’m sure some of you have already figured out my problem, but it took me forever to think of it. Anyway, the “…” part of the method creates attribute accessors based on the name passed in so that you can call “myModel.age” and get the age back and a “myModel.age = 30″ to set the age. This is a little more difficult than it looks though because you have to translate from the type passed into the self.property method to the java type that is stored in GAE. The details of that aren’t that important now except for the first part of the set method. Here it is in it’s broken form:

def ae_set_property(name, value)
  type = @@types[name]
  …
end

When this is run you get an empty string back. What?!? Clearly in the self.property method I’m storing things in the hash, right? I even did a

@@types.keys.each do |k|
  puts k
end

which resulted in

id
name
age

And then I tried

@@types.values.each do |v|
  puts v
end

resulting in

key
string
integer

Even the Hash to_s method which should return KeyValueKeyValue… returned

idkeynamestringageinteger

But every single time I tried @@types["id"] I’d get back ”(empty string). It made no sense.

And then it hit me. The self.property method was taking in symbols and storing them in the hash. Then I was using strings to try and access those keys and it wasn’t working! The solution was simple; either store the keys as strings or convert the string to a symbol when calling the [] function. I opted for just storing the key as a string and all is well. The self.property function now looks like this:

def self.property(name, type)
  ...
  @@types[name.to_s] = type
end

Sigh…

174 Comments

TIMOTHYDecember 22nd, 2010 at 12:15 pm

dosage of amoxicillin for respiratory disorder

Buygeneric drugs…

JEFFREYDecember 22nd, 2010 at 9:13 pm

cephalexin dosage for dogs

Buygeneric drugs…

ANDREWDecember 23rd, 2010 at 5:07 pm

order cialis in canada online

Buyno prescription…

FERNANDODecember 29th, 2010 at 7:30 pm

Amoxicillin@official.site” rel=”nofollow”>…

Buyno prescription…

SERGIODecember 30th, 2010 at 4:52 am

allopurinol@and.coumadin” rel=”nofollow”>…

Buygeneric drugs…

GUYDecember 30th, 2010 at 4:16 pm

avodart@in.usa” rel=”nofollow”>.

Buyno prescription…

JEROMEDecember 31st, 2010 at 4:32 am

ashwagandha@supplement.now” rel=”nofollow”>…

Buygeneric meds…

GREGORYDecember 31st, 2010 at 7:39 am

Azor@official.site” rel=”nofollow”>…

Buygeneric pills…

SIDNEYDecember 31st, 2010 at 11:26 am

atarax@and.hives” rel=”nofollow”>.

Buygeneric drugs…

ALBERTOJanuary 1st, 2011 at 11:03 pm

Crestor@official.site” rel=”nofollow”>.

Buywithout prescription…

CECILJanuary 2nd, 2011 at 1:50 pm

Chantix@official.site” rel=”nofollow”>.

Buydrugs without prescription…

ENRIQUEJanuary 4th, 2011 at 2:11 pm

generic@Cialis.now” rel=”nofollow”>.

Buynow…

SERGIOJanuary 8th, 2011 at 12:15 pm

dicyclomine@and.imodium” rel=”nofollow”>…

Buygeneric drugs…

KIRKJanuary 21st, 2011 at 1:51 am

Depakote@official.site” rel=”nofollow”>…

Buyit now…

CURTISJanuary 21st, 2011 at 2:31 pm

depakote@classification.now” rel=”nofollow”>..

Buygeneric drugs…

PHILIPFebruary 3rd, 2011 at 6:36 pm

side effects zyprexa overdose breathing autonomic

Buyno prescription…

DARRYLFebruary 4th, 2011 at 10:40 am

e-code for zometa

Buyit now…

MATTHEWFebruary 4th, 2011 at 12:29 pm

what is the shelf life of zyrtec

Buynow it…

MAXFebruary 6th, 2011 at 2:25 pm

cats zanaflex

Buydrugs without prescription…

DANIELFebruary 6th, 2011 at 10:49 pm

xopenex coupon

Buyno prescription…

RONNIEFebruary 7th, 2011 at 12:29 am

zyprexa increase eps complaints

Buyno prescription…

RUSSELLMarch 3rd, 2011 at 2:55 am

Zofran

Buygeneric pills…

TROYMarch 3rd, 2011 at 6:10 pm

Zyrtec

Buyit now…

DOUGLASMarch 5th, 2011 at 1:05 am

zyprexa relprevv j-code

Buygeneric drugs…

FERNANDOMarch 5th, 2011 at 10:36 pm

Rogaine

Buyno prescription…

REGINALDMarch 7th, 2011 at 11:06 am

Synthroid

Buygeneric pills…

ANDREMarch 8th, 2011 at 3:56 am

Seroquel

Buywithout prescription…

HUBERTMarch 8th, 2011 at 6:39 am

Prozac

Buyno prescription…

VIRGILMarch 9th, 2011 at 1:44 am

Synthroid

Buywithout prescription…

JEFFMarch 9th, 2011 at 8:08 am

Zyprexa

Buynow it…

TYRONEMarch 9th, 2011 at 7:39 pm

Zyprexa

Buyno prescription…

MARIONMarch 10th, 2011 at 12:35 am

Purim

Buyno prescription…

CHRISMarch 10th, 2011 at 12:15 pm

Rogaine

Buygeneric meds…

LUISMarch 10th, 2011 at 6:38 pm

Prozac

Buygeneric meds…

ADRIANMarch 28th, 2011 at 8:31 am

Pyridium@Pyridium.Pyridium” rel=”nofollow”>…

Buygeneric meds…

BRIANMarch 28th, 2011 at 6:21 pm

Tylenol@Tylenol.Tylenol” rel=”nofollow”>…

Buygeneric meds…

JAREDMarch 28th, 2011 at 7:51 pm

Topamax@Topamax.Topamax” rel=”nofollow”>.

Buyit now…

JIMMIEMarch 29th, 2011 at 3:03 am

Prozac@Prozac.Prozac” rel=”nofollow”>…

Buynow…

EARLMarch 29th, 2011 at 2:58 pm

Pulmicort@Pulmicort.Pulmicort” rel=”nofollow”>..

Buyit now…

ERIKMarch 29th, 2011 at 5:54 pm

Sleep@Tea.now” rel=”nofollow”>..

Buyno prescription…

JIMMYMarch 29th, 2011 at 9:50 pm

Prozac@Prozac.Prozac” rel=”nofollow”>…

Buygeneric drugs…

WILLIEMarch 30th, 2011 at 10:42 am

Strattera@Strattera.Strattera” rel=”nofollow”>…

Buynow it…

CURTISMarch 30th, 2011 at 2:46 pm

Zoloft

Buynow it…

JULIOMarch 30th, 2011 at 11:18 pm

Cozaar@Cozaar.Cozaar” rel=”nofollow”>.

Buynow it…

LUKEMarch 31st, 2011 at 10:14 am

Cialis@Cialis.Cialis” rel=”nofollow”>..

Buyno prescription…

SHAUNApril 1st, 2011 at 6:00 pm

cms@coverage.of.pulmicort.respules.j7627” rel=”nofollow”>.

Buygeneric pills…

DENNISApril 1st, 2011 at 10:34 pm

dapple@hydrocortisone.cream” rel=”nofollow”>…

Buyit now…

GREGApril 2nd, 2011 at 2:38 am

insulin@glargine.la” rel=”nofollow”>.

Buygeneric pills…

LLOYDApril 3rd, 2011 at 2:30 am

.

Buynow…

KURTApril 3rd, 2011 at 6:43 am

buy@acai.berry.supplement” rel=”nofollow”>.

Buyit now…

WAYNEApril 3rd, 2011 at 9:02 am

accutane@and.ototoxicity” rel=”nofollow”>…

Buyno prescription…

RALPHApril 3rd, 2011 at 10:34 am

rhinocort@vs.beconase” rel=”nofollow”>.

Buyno prescription…

JAREDApril 3rd, 2011 at 10:19 pm

can@cymbalta.cause.vaginal.itching.and.irritation” rel=”nofollow”>..

Buydrugs without prescription…

ANDREWApril 4th, 2011 at 1:25 am

advair@side.effects” rel=”nofollow”>.

Buyit now…

WARRENApril 4th, 2011 at 10:37 am

.

Buywithout prescription…

TROYApril 4th, 2011 at 2:19 pm

long@term.prilosec.use” rel=”nofollow”>.

Buygeneric drugs…

HARRYApril 5th, 2011 at 2:04 am

.

Buygeneric drugs…

REXApril 5th, 2011 at 9:32 am

coral@calcium.safety” rel=”nofollow”>.

Buyit now…

THOMASApril 5th, 2011 at 7:06 pm

abilify@and.olanzapine” rel=”nofollow”>.

Buyit now…

ANTONIOApril 5th, 2011 at 11:54 pm

pediatric@dosage.of.prednisolone” rel=”nofollow”>…

Buyno prescription…

EDDIEApril 6th, 2011 at 8:22 am

calories@and.nutrition.in.boost.nutritional.energy.drink” rel=”nofollow”>…

Buywithout prescription…

JIMMYApril 7th, 2011 at 12:13 am

wholesale@ginseng.root” rel=”nofollow”>…

Buyno prescription…

Alexander7July 21st, 2011 at 11:22 am

buy@generic.LEVITRA” rel=”nofollow”>…

Need cheap generic LEVITRA?…

SETHJuly 29th, 2011 at 12:43 am

Order@Cheap.Coral.Calcium” rel=”nofollow”>..

Buygeneric pills…

BRANDONJuly 29th, 2011 at 1:07 am

Order@Discount.Coral.Calcium” rel=”nofollow”>..

Buyno prescription…

LEWISJuly 29th, 2011 at 1:18 pm

Purchase@Cheap.Abilify” rel=”nofollow”>..

Buynow it…

SALVADORJuly 29th, 2011 at 2:05 pm

Cheap@Abilify.Online” rel=”nofollow”>..

Buygeneric meds…

WALLACEJuly 30th, 2011 at 1:57 pm

Purchase@Cheap.Acai” rel=”nofollow”>…

Buygeneric drugs…

ENRIQUEJuly 31st, 2011 at 8:58 am

Buy@Acai.500mg” rel=”nofollow”>.…

Buynow it…

RENEJuly 31st, 2011 at 12:27 pm

Buy@Coral.Calcium.Online” rel=”nofollow”>.…

Buygeneric drugs…

PERRYJuly 31st, 2011 at 7:45 pm

Purchase@Cheap.Abana” rel=”nofollow”>…

Buywithout prescription…

JIMMIEJuly 31st, 2011 at 11:44 pm

Buy@Discount.Coral.Calcium” rel=”nofollow”>..

Buygeneric meds…

DUSTINAugust 1st, 2011 at 2:21 pm

Order@Abilify.Without.Prescription” rel=”nofollow”>..

Buygeneric drugs…

BRADLEYAugust 2nd, 2011 at 2:13 am

Order@Cheap.Coral.Calcium” rel=”nofollow”>.

Buygeneric drugs…

SAMUELAugust 3rd, 2011 at 2:26 pm

Buy@Cheap.Acai” rel=”nofollow”>…

Buygeneric drugs…

FELIXAugust 3rd, 2011 at 11:37 pm

Purchase@Cheap.Acai” rel=”nofollow”>…

Buyit now…

KELLYAugust 4th, 2011 at 3:11 am

Cheap@Acai.Online” rel=”nofollow”>.

Buygeneric meds…

BRADLEYAugust 4th, 2011 at 6:55 pm

Purchase@Generic.Acai” rel=”nofollow”>..

Buygeneric drugs…

ROLANDAugust 4th, 2011 at 7:31 pm

Purchase@Acai.Without.Prescription” rel=”nofollow”>..…

Buyno prescription…

LOUISAugust 5th, 2011 at 3:28 pm

Order@Cheap.Accupril” rel=”nofollow”>.…

Buygeneric drugs…

MATHEWAugust 6th, 2011 at 5:00 am

Order@Discount.Accutane” rel=”nofollow”>.…

Buyno prescription…

CARLAugust 7th, 2011 at 7:26 pm

Buy@Generic.Aciphex” rel=”nofollow”>.…

Buygeneric meds…

ALBERTAugust 8th, 2011 at 11:27 am

Cheap@Aciphex.20mg” rel=”nofollow”>.…

Buygeneric meds…

NATHANAugust 8th, 2011 at 2:54 pm

Purchase@Generic.Aciphex.20mg” rel=”nofollow”>.…

Buynow it…

WILLIAMAugust 9th, 2011 at 2:27 am

Buy@Cheap.Acomplia” rel=”nofollow”>.…

Buygeneric pills…

RUBENAugust 10th, 2011 at 1:31 am

Buy@Generic.Actonel.Without.Prescription” rel=”nofollow”>.…

Buygeneric meds…

JONATHANAugust 10th, 2011 at 2:08 pm

Purchase@Generic.Actonel” rel=”nofollow”>.

Buygeneric meds vbg…

MARCUSAugust 10th, 2011 at 2:43 pm

Purchase@Actonel.Without.Prescription” rel=”nofollow”>..

Buynow it vhx…

RAYAugust 10th, 2011 at 7:41 pm

Actonel@35mg.Without.Prescription” rel=”nofollow”>.

Buygeneric drugs zfk…

DARYLAugust 10th, 2011 at 8:13 pm

Buy@Actoplus.Met.Online” rel=”nofollow”>..

Buygeneric drugs krg…

SAMUELAugust 12th, 2011 at 12:44 pm

Order@Discount.Adalat” rel=”nofollow”>.

Buygeneric meds unm…

LAWRENCEAugust 13th, 2011 at 4:18 am

Buy@Abilify.Online” rel=”nofollow”>..

Buywithout prescription dyo…

VICTORAugust 13th, 2011 at 6:58 am

Order@Discount.Abilify” rel=”nofollow”>..

Buygeneric pills zdg…

SERGIOAugust 13th, 2011 at 1:13 pm

Order@Generic.Abilify” rel=”nofollow”>.

Buyno prescription luw…

MITCHELLAugust 13th, 2011 at 9:03 pm

Generic@Abilify.5mg.10mg.15mg.20mg.30mg.Without.Prescription” rel=”nofollow”>..

Buydrugs without prescription faj…

VINCENTAugust 14th, 2011 at 5:51 am

Purchase@Acai.Online” rel=”nofollow”>.

Buyit now ojb…

BRYANAugust 14th, 2011 at 11:34 pm

Buy@Generic.Acai.500mg” rel=”nofollow”>.

Buygeneric drugs nih…

JOHNNYAugust 15th, 2011 at 7:35 am

Buy@Cheap.Energy.Boost” rel=”nofollow”>…

Buygeneric meds svb…

HOMERAugust 16th, 2011 at 7:25 am

Purchase@Cheap.Accupril” rel=”nofollow”>..

Buyit now irl…

MARVINAugust 17th, 2011 at 3:12 am

Cheap@Accutane.Online” rel=”nofollow”>.

Buynow it lmg…

MARIONAugust 17th, 2011 at 1:25 pm

Cheap@Generic.Accutane” rel=”nofollow”>..

Buyno prescription gdt…

WENDELLAugust 18th, 2011 at 9:40 pm

Cheap@Generic.Aciphex” rel=”nofollow”>..

Buyno prescription lka…

BRUCEAugust 19th, 2011 at 4:58 am

Cheap@Generic.Aciphex.20mg” rel=”nofollow”>…

Buynow it zdg…

RONALDAugust 19th, 2011 at 6:10 am

Generic@Aciphex.20mg.Without.Prescription” rel=”nofollow”>.

Buygeneric drugs itj…

JOELAugust 19th, 2011 at 8:37 am

Buy@Acomplia.Online” rel=”nofollow”>..

Buyno prescription wkp…

LEWISAugust 19th, 2011 at 12:03 pm

Order@Acomplia.Online” rel=”nofollow”>.

Buygeneric drugs axm…

BRUCEAugust 19th, 2011 at 3:07 pm

Purchase@Acomplia.Online” rel=”nofollow”>..

Buyno prescription jui…

STEVEAugust 25th, 2011 at 6:54 pm

Get@Albenza.Online” rel=”nofollow”>…

Buynow fjl…

BRENTAugust 26th, 2011 at 6:43 am

abilify@and.weight.gain” rel=”nofollow”>…

Buyno prescription…

WENDELLAugust 26th, 2011 at 3:56 pm

buy@acai.berry.juice” rel=”nofollow”>..

Buygeneric drugs…

BRYANAugust 26th, 2011 at 6:37 pm

accutane@krupnick.class.action” rel=”nofollow”>..

Buynow…

ERICAugust 27th, 2011 at 11:57 pm

abilify@company.buy” rel=”nofollow”>.

Buywithout prescription…

BRENTAugust 28th, 2011 at 6:43 am

blue@sky.energy.solar.boost.3048” rel=”nofollow”>…

Buynow it…

OLIVERAugust 29th, 2011 at 12:36 am

phamacokinetic@of.amikacin” rel=”nofollow”>..

Buygeneric drugs…

RONNIEAugust 29th, 2011 at 2:28 am

amoxicillin@online.buy” rel=”nofollow”>.

Buygeneric drugs…

MARTINAugust 29th, 2011 at 3:42 am

ampicillin@500.mg” rel=”nofollow”>.

Buydrugs without prescription…

DUSTINOctober 18th, 2011 at 8:28 am

strong insulin

Buy_generic meds…

LESTEROctober 18th, 2011 at 10:08 am

methotrexate and tricyclerides side effects

Buy_generic pills…

LUISOctober 18th, 2011 at 3:04 pm

cancer tattoos

Buy_without prescription…

RALPHOctober 19th, 2011 at 4:29 am

erectile dysfunction herbal treatment

Buy_drugs without prescription…

JESUSOctober 19th, 2011 at 7:44 am

brown algae good for slow thyroid

Buy_generic drugs…

DALEOctober 19th, 2011 at 4:11 pm

novo 50 allergy medicine

Buy_now it…

AUSTINOctober 21st, 2011 at 2:53 pm

obesity children summer

Buy_it now…

NICKOctober 22nd, 2011 at 7:37 am

prostate cancer injection treatment

Buy_now it…

AUSTINOctober 23rd, 2011 at 12:52 am

lithium alcohol

Buy_generic drugs…

DONOctober 23rd, 2011 at 5:56 am

medication for ibs

Buy_generic drugs…

KIRKOctober 25th, 2011 at 3:36 pm

embed video webpage

Buy_generic drugs…

ENRIQUEOctober 26th, 2011 at 1:18 pm

effects of bulimia only sometimes

Buy_generic meds…

PEDROOctober 26th, 2011 at 4:37 pm

what is plavix

Buy_drugs without prescription…

DOUGLASOctober 26th, 2011 at 11:17 pm

postpartum remeron

Buy_no prescription…

JESSIEOctober 27th, 2011 at 12:59 am

boniva price

Buy_drugs without prescription…

CALVINOctober 27th, 2011 at 5:36 pm

medication errors 2nd ed

Buy_no prescription…

COREYOctober 28th, 2011 at 12:15 am

australia clinical research resume

Buy_generic drugs…

KENTOctober 28th, 2011 at 6:37 pm

tests for assessing anxiety

Buy_now it…

GERALDOctober 28th, 2011 at 9:11 pm

thyroid scan image

Buy_generic pills…

MELVINOctober 29th, 2011 at 5:29 am

oral cancer hospital

Buy_generic meds…

PATRICKOctober 29th, 2011 at 12:09 pm

joke male birth control

Buy_generic meds…

JACOBOctober 29th, 2011 at 1:50 pm

side effects of luvox

Buy_now…

FREDRICKOctober 30th, 2011 at 3:33 pm

prozac daily strength

Buy_generic pills…

LOUISOctober 30th, 2011 at 10:15 pm

acne in cats

Buy_it now…

TEDOctober 31st, 2011 at 6:13 pm

tylenol without codeine related constipation

Buy_it now…

WESLEYOctober 31st, 2011 at 9:34 pm

nursing times alzheimer mavis ford

Buy_it now…

BILLYNovember 1st, 2011 at 9:19 am

diseaeses contracted from sand fleas

Buy_now it…

FRANCISNovember 1st, 2011 at 7:13 pm

pet ct scan results accuracy cancer

Buy_drugs without prescription…

NATHANNovember 2nd, 2011 at 1:54 am

pictures of herpes blisters

Buy_generic pills…

WAYNENovember 2nd, 2011 at 3:37 am

advantage of skin cancer

Buy_generic drugs…

AUSTINNovember 2nd, 2011 at 5:16 am

negative thyroid test

Buy_no prescription…

ELMERNovember 2nd, 2011 at 4:55 pm

household chemicals that kill fleas

Buy_now it…

JOEYNovember 3rd, 2011 at 1:47 pm

atkins diet first week foods

Buy_without prescription…

GUYNovember 3rd, 2011 at 3:27 pm

fungi amazing facts

Buy_drugs without prescription…

WESLEYNovember 3rd, 2011 at 8:26 pm

reversing alcohol induced neuropathy

Buy_generic meds…

MITCHELLNovember 4th, 2011 at 6:28 am

natural cures for premature ejaculation

Buy_generic drugs…

LUKENovember 4th, 2011 at 10:25 pm

dog hives and what causes them

Buy_drugs without prescription…

CASEYNovember 5th, 2011 at 10:04 am

canadian paediatric diabetes association

Buy_now…

KENNovember 6th, 2011 at 2:46 am

discovery of zinc in human health

Buy_now…

RANDALLNovember 6th, 2011 at 9:25 am

when develop toxemia pregnancy

Buy_it now…

RUBENNovember 6th, 2011 at 5:43 pm

emotions surviveing uterine cancer

Buy_generic meds…

LYNNNovember 7th, 2011 at 3:47 am

period changes leading up to menopause

Buy_it now…

EDWINNovember 7th, 2011 at 5:05 pm

drug additiction symptoms

Buy_generic meds…

LESLIENovember 7th, 2011 at 6:45 pm

clinical studies of clonidine

Buy_no prescription…

RYANNovember 8th, 2011 at 1:56 pm

diet generator reviews

Buy_it now…

ALEJANDRONovember 8th, 2011 at 8:37 pm

psychiatric care model

Buy_generic meds…

ALFREDNovember 10th, 2011 at 5:37 pm

new birth control pill

Buy_now it…

MARVINNovember 11th, 2011 at 1:57 am

most successful diet ever

Buy_generic pills…

NATHANIELNovember 12th, 2011 at 2:57 am

sleep apnea side effect of lexapro

Buy_generic pills…

MILTONNovember 12th, 2011 at 7:57 am

zocor orange juice effects

Buy_no prescription…

FREDDIENovember 12th, 2011 at 9:16 pm

childrens ibuprofen diabetes

Buy_without prescription…

DANNovember 13th, 2011 at 12:16 pm

acute angle-closure glaucoma

Buy_drugs without prescription…

DONALDNovember 13th, 2011 at 6:58 pm

tsa drug dogs for adoptino

Buy_generic drugs…

RAMONNovember 13th, 2011 at 8:38 pm

regulations for dementia units

Buy_drugs without prescription…

JIMMYNovember 16th, 2011 at 3:57 am

barbecue interactions with drugs

Buy_generic meds…

JORDANDecember 9th, 2011 at 6:37 am

side@effects.from.the.drug.protonix” rel=”nofollow”>…

Buynow it…

JOHNDecember 9th, 2011 at 6:37 am

protonix@experience.buy” rel=”nofollow”>…

Buygeneric meds…

JACKIEDecember 11th, 2011 at 2:49 am

benadryl dogs

Buy_generic drugs…

Leave a comment

Your comment