Friday, December 25, 2009

In Ruby Arrays are also Queues

In Ruby it is safe to add an element to the end of an array during iteration.

a = [0,1,2]
a.each do |b|
  puts b
  if 1 == (b % 2)
    a << a.last + 1
Ranges as are not arrays.
a = (0..2)

Saturday, December 12, 2009

Ubuntu Internet Connection Sharing

This is one of those occasions for the Staple's "That was easy" button.

Right-click on the network icon
Edit connections
Select Eth[your secondary port]
Change the name to ICS or whatever to help you remember
Click the IPV4 tab
Method: Shared to other computers.

By default it does NAT, not bridging, so your connected computer will get a 10.x.x.x address, not a 192.168.x.x.

It beats setting up a dd-wrt as an ethernet bridge by a small margin. However, that doesn't look too difficult either. I just didn't have the time to fiddle with it.

Sunday, December 6, 2009

[Ruby on Rails] Error 406: Not Acceptable

You know what's "Not Acceptable"? Not having a more meaningful error message!

One probable cause for a 406 error is that you're requesting an invalid format.

For example, let's say you're requesting application/json from /model.json and you've got a block of code like:
respond_to |format|
    format.xml { render :xml => @model }
    format.xml { render :xml => @model.errors }

That's a problem.

And you're not getting a response back telling you what the error is because the error is the server notifying your client that it can't accept the response...... infinite loop.... anyway, you've forgotten to add in something like this:
    format.json { render :json => @model.errors }

Next you'll probably get a 422, but that just means something is wrong with the data saving itself. At least in that case you'll have a decent error message to look at.


Friday, December 4, 2009

A quick rundown on PHP Symfony directory hierarchy

I would rather that Symfony had chosen a quick folder hierarchy, but they didn't. They chose the most hierarchical convoluted structure that they could think of and then added a few extra directories. Actually, it works quite well in terms of how component-agnostic (and multi-component-friendly) it is.

However, today I had the opportunity to explain it to someone for the first time and hence I finally understood it for the first time.

This is what I deemed to be of particular importance:


This represents the extension of one object in your database table. This is where you put your calculated values and temporary variables for any logic that belongs to the object itself.


This represents the table. This is where you create your NoSQL statements with joins and such using the Doctrine syntax.


This represents the class of one object. This maps the object to the database. You don't edit this, you edit the extension above.


If you visit it will call the method executeAction here.


This represents the html template that you action calls.

models, controllers, views

Symlinks that I create to lib/model/doctrine/, app/frontend/modules/lower_class_name/actions and app/frontend/modules/lower_class_name/templates/ so that I know what the heck it is I'm doing and don't have to thumb through a billion directories... except I don't actually do that... but if I ever get heavy into symfony I may very well create a script that does... ugh, then there's the filter and forms and such too...


This is a link to ~/Code/project_name/web, and is where you put your static files - images, css, and such.

Ruby has a much cleaner directory structure... but I don't think it's as easy to do complex things in Rails as it is in Symfony.

Thursday, December 3, 2009

Cross-Origin Resource Sharing (CORS) aka valid Cross-Site Scripting (XSS)

Ever tried to create a web application that could feasibly work from any other website (or even the desktop) without the client having to set up anything - a sort of pluggable widget? Ugh...

I'd like to have a nice large table for the various scenarios and browsers, but until then I'll just make my notes here.

AJAX Cross Site

GET allows only application/json, application/javascript, text/javascript
GET may have a body (thus emulating an AJAX post), except in IE
POST is forbidden
HEADERS forbid X-HTTP-Method-Override
CORS is supported by all modern browsers except Opera

HTML Cross Site

GET allows application/json, application/javascript, text/javascript
GET forbids text/plain

POST allows application/json, application/x-www-form-urlencoded, multipart/form-data
POST forbids none


Everything should be supported
HEADERS allow X-HTTP-Method-Override