Tuesday, January 20, 2009

Central Square Library -20JAN2009

Agenda
Active Record and Method Missing

Venue

Today's class is the second to last class here at the CS Library.

We are still getting booted:
Alternatives are:
MIT?
Toscaninis
Somerville, Cambridge, Brookline
Conference Call
Does everyone have skype, a camera, would it work?

Glenn's using IRB*
* NOTE: There was some talk that Glenn was using IRB for his editting. he isn't.
He is using ERB though.

require 'rubygems'
require 'active_record'

It is possible to build a connection and make updates to different database platforms easily.

#Active record translates the ruby to sql.

ActiveRecord::Base.establish_connection(

:apdapter=>"mysql",

mysql is the database Glenn's been using
You can pass sql in Ruby, but you can't use active record.
If there's an issue with speed and peerformance.

:host=>"localhost",
:password=>"xxx",
:database=>"rubyrx")

This previous stuff is a hash btw.

class DM < distinc_records="DM.find(:all,:select="">"distinct trtgrp ,#{on_var}")

Give me all the records from this object that fit fit this criteria.

Create an array that is equal to

M P

M D

M P

M D

F P

F D

on something like this:

['M','F','D','P')

To get an array of all the activerecord objects which correspond to the above.

To get all records drop the distinct.
records=DM.find(:all)

Each variable is a column in table and each object in the array is a row.

We can iterate over distinct.record.

distinct_records.each do r
sex_by_trtgrt=SummaryStatistics.new
end

Change from the way Glenn did things last week.
Abstract out the statistical part of the code.

Why did Gklenn more from args to onvar

distinct_records.each do r
instance_eval("#on_var)_by_trtgrp[r.trtgrp]=SummaryStatistics.new")
end

Creates a key in the hash and a value.
{'Placebo=> <>, 'Drg' => <>}
Sets up the key pair relationship.


o=Object.new
puts o
would return something like <71343427>

@sex_by_trtgrp

Summary Statistics


class SummaryStatistics
def initialize
@arr=[]
end
def cal_n
...
end
mean, median, max, min

def <<(obj) @arr<["M"],'DRUG'=>['M','M','F']

There are no calculations, only pushing the values into the hash.

This part does one thing and this part dose another. If you change one part you don't have to change the other.

r.trtgrp is the value of trtgrp for that row.


Next Week
Natural Language Parsing

Future
Logging
IO
Test Unit
Spec

Glenn recommends going through the code he just post to really understand this.
He's thinking of posting to Heroku.
Performance is slow.
1000 is a about a second.
Over 10,000 might be a problem about 60 second
Its not horrible but it is not great.

Next week we'll get more SummaryStatistics

No comments: