elz: (ada-reboot)
elz ([personal profile] elz) wrote2010-05-03 05:05 pm
Entry tags:

Because I found it strangely interesting



Enigel found a 500 error in our errors archive that came up when someone tried to title a work "08". This was odd, since "07" and "007" worked just fine.

She tracked it to this line, which is part of the code for title sorting for works (so that they ignore 'a', 'an' and 'the' at the beginning of the title and make titles starting with numbers sort numerically):

sorted_title.gsub(/^(\d+)/) {|s| "%05d" % s}

That's some pretty complicated code, so first we have to figure out what it does. It breaks down sort of like this:

1) sorted_title is a string, so it could be "08", "bob", "And Then There Were None", etc.

2) gsub is a method that uses regular expressions to replace part of a string with something else. "bob".gsub(/b/, "w") would give you "wow", for instance. In this case, instead of having a second parameter, it uses a block. More on that in a second.

3) /^(\d+)/ is a regular expression pattern. The opening and closing forward slashes just mark the beginning and end of the pattern. The carat (^) means "the beginning of a line". The backslash plus d (\d) means "a digit from 0-9". The plus sign means "one or more of the thing right before me". So all of this adds up to "one or more digits that come right at the beginning of the line" (because for sorting purposes, we only care about the first word or number). The parentheses mean "capture this stuff so we can do something to it" -- in this case, all we care about is the number itself.

4) Next we have a block (the code in { }). |s| is a way of giving a variable name to each item that gets passed into the block. We could call it anything: |number| or |thingamajig| or |harry| - the name itself doesn't mean anything. We'll do things with s in the block, and whatever value the block returns becomes the replacement for the original number in the title.

5) Inside the block, we have the REALLY fun code. "%05d" % s -- this is shorthand for the sprintf method, which "returns the string resulting from applying format_string to any additional arguments." So basically, take s, apply "%05d" to it, and return the result.

In context, d means "convert to a decimal number", and in this case, it's s that we're converting. The 5 tells it to expand the number to five places, and the 0 tells it to use zeroes to fill out any empty spaces.

So, the goal is to convert things like so:

7 -> 00007
50 -> 00050
001 -> 00001
1999 -> 01999

Which works... most of the time.

Where the bug comes in



I ran the individual code from irb so that I could see what the error was:

ArgumentError: invalid value for Integer: "08"

So I tossed that into Google to see what came up - thankfully, there was a page that described a similar sort of problem. I didn't understand the whole explanation right off the bat, but the gist of it was that sprintf tries to convert the value you give it to a decimal number, and if the value begins with a leading zero, it tries to convert it from octal into decimal. Octal is a number system with eight digits (0-7) the way decimal has ten digits (0-9), so 8 and 9 don't exist in octal, which means they cause an error. And thus anything from "00" to "07" works fine, but "08" or "09" will get you the sadface error page of doom.

The method it uses to do the conversion is the Integer() method, which has a different set of rules from the to_i method that can also be used to convert things to integers. Witness if you will:

> "07".to_i
=> 7
> Integer("07")
=> 7
> "08".to_i
=> 8
> Integer("08")
ArgumentError: invalid value for Integer: "08"


If you have Ruby installed, you can open irb and try that out yourself.

What I noticed while doing that is that not only does "08".to_i not choke, it also strips the leading zeroes, and thus provides a clue to a reasonable solution:

irb(main):005:0> Integer("08".to_i)
=> 8


Woo! Which translates to:

sorted_title.gsub(/^(\d+)/) {|s| "%05d" % s.to_i}

And now you can title all your works "08" and "09". I'm sure everyone will be very excited about that.

(Note: yes, if you begin your titles with numbers that (a) contain commas, or (b) have six or more digits, you can probably make the sorting go all weird. But that's a garden variety bug, so it doesn't get its own post.)

(Note 2: I keep checking to see if there's a better way to do that, but no luck so far with various string and integer methods.)

lian: befuddled budgie is befuddled (huh?)

[personal profile] lian 2010-05-03 09:34 pm (UTC)(link)
that was a strangely fun read.
lian: Klavier Gavin, golden boy (Default)

[personal profile] lian 2010-05-03 09:51 pm (UTC)(link)
I laughed when I arrived at the octal bit. I mean, wtf? It parses in a different numerical system! you got to catch that first leeetle detail o_O;

(also, maaan, there is absolutely no room in my life for learning to code, but it's not as if I don't get the urge now and then. Especially when reading fun tidbits like this!)

zsXlpHigYPH

(Anonymous) 2013-06-11 07:45 am (UTC)(link)
This is what the Govt likes to do in helping the citezins. Help is always there provided you are capable of going thru a load of paper work, process and criterias until you gave up and rely on your own or suffer in silence.Publicly, Govt can say there is avenue to get help but realistically it is not really helping. Minimum wage is a better and more dignified way in helping and setting a minimum standard for people to work for a living. It is time for Govt to seriously look at this.

eUlqBzaUYFQTvOJG

(Anonymous) 2013-06-09 03:09 pm (UTC)(link)
Rex comments as foollws,Bicycle sharing systems in university campuses work because the traffic drop-nodes are clearly defined, e.g Library, Lab, Computer Centre, Lecture Theatre etc. There is fair inter node travel so the bicycles will on balance be spread fairly evenly between nodes. This is a pre-requisite for the sharing system to be successful.To apply the bicyle sharing to a satellite town like ang mo kio, it is therefore necessary to first identify optimised drop-nodes. MRT station, yes. But where are the other nodes? the AMK library.. it's not that popular. The AMK park? not many people go there. The food centre? no that's too new the MRT station... The HDB blocks? Yes... but there are so many blocks. It is like a fan-out from the MRT to numerous hdb blocks spread out all over thE place.. how to have a fair sharing system to design the nodes...Perhaps the bicycle sharing system may better work in Orchard Rd. The nodes are quite simple, ION, 313, Plaza Singapura, City Mall. But yuks! we have another problem there, the cylcists will get squashed by the buses and cars.So the problem is not technology or economics i think. We have to find an acceptable nodal-model in a relatively safe environment, to apply it successfully.Actually a bicycle is very cheap, about $60 for a simple bike even with gear (definitely required to go up slope).. this is the best investment, it will give you the freedom to go back to your home doorstep from the target location, it is green, it saves you money, hardly any maintenance.rex
enigel: Sheppard and McKay huddled around McKay's laptop (SGA McShep laptop love (by me))

[personal profile] enigel 2010-05-03 10:34 pm (UTC)(link)
Eee! A great read. Thanks for putting this into a narrative! :)

PS: is your "re-boot!" icon gankable? :D

FrbSxkrOjXU

(Anonymous) 2013-06-11 08:13 am (UTC)(link)
Quote omitted since my quote tags don't seem to work right.I noitce the mention of abilities that can treat wounds. What about abilities that can restore or temporarily increase HP? Will the charismatic leader be able to inspire someone to fight on in the face of fear? Will giving a rousing speech before battle increase the rest of the parties resolve thus resulting in higher temporary HP or some other mechanic?Edited at 2012-05-25 11:32 pm (UTC)
samvara: Photo of Modesty Blaise with text "All this and brains as well" (Default)

[personal profile] samvara 2010-05-03 11:18 pm (UTC)(link)
...that makes so much sense!
rodo: chuck on a roof in winter (Default)

Here via lian

[personal profile] rodo 2010-05-03 11:29 pm (UTC)(link)
which is part of the code for title sorting for works (so that they ignore 'a', 'an' and 'the' at the beginning of the title and make titles starting with numbers sort numerically)

Would it be possible to add other articles (like ... German ones) to this list? I find it incredibly confusing to look for stroes when they're ordered differently.

PS: Sorry for the typos I likely made. I had to type this blind because of font issues.