I’ve been meaning to try and learn Ruby for a few months now, since I’ve discovered _why’s (poignant) guide. As often when it comes to programming (and about everything, really), nothing replaces practice. Combine that with an idea that floated in my head, and here’s what gives:
http://sowhatcouldigetatmcdonalds.com/
The concept
The idea for this website popped up as I was chatting with fellow students; something along the lines of “hey, wouldn’t it be cool if you had a website that’d told you, like, you know, say, for $24 you can have a bunch of big macs, 12 large fries and a sundae? hu? pretty neat, heh?”.
Their answers, mostly “hmm yeah I guess” and related lukewarm comments were not that encouraging of a feedback; so if you don’t think it’s that neat, don’t worry.
But really, as you probably noticed, the concept of the website is pretty simple. I have some money, you have a McDonalds menu: let’s make that work!
NP-completeness
Well it turned out that this problem is actually NP-complete, as in an O(n^errr okay that shit is hard) kind of complexity – XKCD sums it pretty well. To be more formal, it’s a problem that cannot be solved both fast (in polynomial time) and accurately. A long line of people attempted to maximize their bang for the buck at any restaurant (I only remembered the XKCD strip after starting to code, did I unvolontarily plagiarized it?), and it’s called the ‘knapsack problem‘ – or ‘unbounded knapsack problem’, if you really want to shine at parties.
Algorithm
The best (known) ways of solving the knapsack problem involves some heavy and interesting stuff, like dynamic programming, or even what wikipedia calls a “fully polynomial-time approximation scheme” (I didn’t even check that out, search at your own risks). Considering developing that would already take a great deal of time, I opted for a quite simpler approach, to begin with at least: my algorithm tries to stuff random McDonalds items in until there’s not enough money left. Complex algorithms? To-mah-to, to-mae-to.
Prices
Prices at McDonalds aren’t the same everywhere in the US (let alone the world), considering it’s a franchisee system. Being a truly conscientious and precise man, I decided to use the prices from someone’s picture of a McDonalds menu found randomly on Flickr (I’ll give props if I ever find the original source again).
Behind the scenes
Programming langage: Ruby
Not a lot to say here â I wanted to learn Ruby (because all the cool kids use Ruby!) so I did it in Ruby.
Framework: Sinatra
At first I wanted to use Ruby on Rails, but a bit of digging showed that Rails doesn’t really make sense for a project that small. I still plan on learning it, but Sinatra is incredible to quickly design and launch websites.
Templates: Haml & Sass
Haml and Sass are really great templating engines. Following Ruby’s approach of simplicity, minimalism and DRY, they allow you to write darn clean code that will be interpreted in HTML and CSS at runtime by Ruby (using the haml gem).
Hosting: Heroku / OVH
Heroku is hands down the simplest way to have your Sinatra app online. I would gladly repeat their sales pitch, but their website looks cooler than mine (damn pro infographists): http://heroku.com/
I also list OVH here because I registered http://sowhatcouldigetatmcdonalds.com/ through OVH’s registrar. 7⬠for a year is perfect for me, and apparently even cheaper than GoDaddy.
Deployment: Git
If it’s on Heroku, it’s on Git.
I wish I used Git more during the development itself (it’s designed for source control after all), but I ended up not using it much before actually pushing the application on Heroku and GitHub.
Show me the code!
Sure! Here it is: http://github.com/Pluies/sowhatcouldigetatmcdonalds
Misc
The XHTML Transitional code generated by Haml is all neat (yay!) and passes the W3C validator (yay!) – same for the CSS (yay!). The CSS code is a little messy in my opinion though because the parens are closed inline, but I guess it’s a matter of taste.
The little bit of Javascript used to highlight the divs and generate the whole “not lovin’ it” part uses jQuery (through Google’s CDN).
—
I’ll post more about the process of getting used with all the tools in a quite-near future. In the meantime, I’m looking for any comment, criticism or advice on that project. Tell me everything!
Cool project! I want to build one that does the knapsack problem, as I routinely have very few dollars and want to maximize caloric intake :-)