Posts

Showing posts from February, 2008

ActiveSupport::CoreExtensions::String::Inflections underscore

>> "ActiveSupport::CoreExtensions::String::Inflections".underscore => "active_support/core_extensions/string/inflections" >> "ActiveSupport::CoreExtensions::String::Inflections".underscore.camelize => "ActiveSupport::CoreExtensions::String::Inflections" Looks like Rails extension to the string class does more than underscore a string. Which is useful, but shouldn't the name be "relative_path" or something similar? When I see the method name "underscore" I know exactly what it means, except that's not what it does. The opposite of "underscore" is "camelize " which, again, may not have the best name. And if you want to find the declared constant specified in a string: >> "ActiveSupport::CoreExtensions::String::Inflections".constantize => ActiveSupport::CoreExtensions::String::Inflections Useful and an excellent name.

Return to Generating a Unique Number

In a previous post I discussed generating a unique id number for an order in our database. Read the original post for the full story, but to sum up, I decided that playing around with rand(100_000_000) was good enough. Martin Ankerl posted a comment pointing out that I was vulnerable to the Birthday Problem . So after a quick trip to wikipedia, I found out that if you have 23 people in a room you might think you have a 23/365 chance of having 2 people with the same birthdays, but no, your chances are closer to 50%! This is because in a group of 23 people there are 253 pairs each of which have a 1/365ish chance to have the same birthday. The formula to calculate how many are needed to give you a 50% chance of having a birthday collision is: .5 + sqrt(.25 + 2 * 365 * ln(2) ) = 22.9999 Which means the 50% chance of collision mark for 100,000,000 is: .5 + sqrt(.25 + 2 * 100,000,000 * ln(2) ) = 11774.600235771 Holy crap! I'm screwed if I can only have 12,000 orders before I get a...