Archive for December, 2007

javascript array manipulation

>>> a= [1,2,3]
[1, 2, 3]
>>> a[a.length] = 4
4
>>> a
[1, 2, 3, 4]

>>> foo = { foo: "foo" }
Object foo=foo
>>> bar = { bar: "bar" }
Object bar=bar
>>> a = [ foo, bar ]
[Object foo=foo, Object bar=bar]
>>> a.without(foo)
[Object bar=bar]

{}.to_json vs. OpenStruct.new({…

>> h = { :foo => "foo", :bar => 1 }
=> {:foo=>"foo", :bar=>1}
>> h.to_json
=> "{\"foo\": \"foo\", \"bar\": 1}"
>> require 'ostruct'
=> ["OpenStruct"]
>> o = OpenStruct.new(h)
=> #
>> o.to_json
=> “{\”table\”: {\”foo\”: \”foo\”, \”bar\”: 1}}”

# make it play nicely with to_json

>> class OpenStruct
>>   def to_json
>>     table.to_json
>>   end
>> end
=> nil
>> o = OpenStruct.new
=> #
>> o.foo = “foo”
=> “foo”
>> o.bar = 1
=> 1
>> o.to_json
=> “{\”foo\”: \”foo\”, \”bar\”: 1}”

lowpro autosave behavior to POST JSON

Event.addBehavior({
  '.autosave': Behavior.create({
    onchange : function() {
      var source = this.element;
      var name = source.name.split('['); // name according to rails convention
      var model = name[0]; // rails model name
      var resource = model + 's'; // rails resource (TODO better pluralization)
      var id = name[1].substring(0, name[1].length - 1); // rails model id

      var authToken = 'authenticity_token=' + $('auth-token').getValue()
      if(name[1].match(/new/)) {
        var url = '/' + resource + '?' + authToken
      } else {
        var url = '/' + resource + '/' + id + '?_method=put&' + authToken;
      }

      var inputs = $(source.parentNode).id.split('-');

      new Ajax.Request(url, {
        method: 'post',
        contentType: "application/json",
        postBody: Object.toJSON({
          survey_execution_id: $('survey_execution_id').getValue(),
          question_application_input_id: inputs[1],
          time_line_offset: inputs[2],
          value: source.getValue()
        })
      });
    }
  });
});

sex spam

“Grow an anaconda out of your trouser snake!” — Santiago Maxwell - spammin’ prick

string interpolation

>> "%s %s" % ['Hello', 'World']
=> "Hello World"

the main thing you don’t learn with a cs degree

The main thing you don’t learn with a CS degree is how to develop software, although you will probably build up certain muscles in your brain that may help you later if you decide that developing software is what you want to do.

– Joel Spolsky

sex spam

“If your warrior of love is too small, you may lose this war” — Augusta Lassiter - spammin’ bitch

reading csv files with ruby

require 'csv'
CSV::Reader.parse(File.open('filename.ext', 'r'), ';') do |row|
p row
end