Posts

variables - Where should I store calculated values I use throughout my application in Rails? -

i have following variable definition: @monday = (time.now).at_beginning_of_week i use in different models, controllers, , views. where can define (and how should define -- @@? ) can define once , use throughout rails application? should in environment.rb? should @@? i add application controller: before_filter :load_date def load_date @monday = (time.now).at_beginning_of_week end so accessible in of yours controllers , views. if want use in models, need param, on example scopes. controller place should pass variable model: @models = mymodel.before_date(@monday) i don't think need have 1 instance of variable whole application. initializing quite simple. not when initialize , don't use it. me hard imagine need in of controllers , actions. the other way can define class: class mydate def self.get_monday time.now.at_beginning_of_week end end and put in config/initializers (probably there better place put it). can access anywhere in a...

Finding the longest down sequence in a Java array -

given array int [] myarray = {5,-11,2,3,14,5,-14,2}; i must able return 3 because longest down sequence 14,5,-14. what's fastest way this? ps: down sequence series of non-increasing numbers. just make 1 pass through list of numbers. pseudocode: bestindex = 0 bestlength = 0 curindex = 0 curlength = 1 index = 1..length-1 if a[index] less or equal a[index-1] curlength++ else //restart @ index since it's new possible starting point curlength = 1 curindex = index if curlength better bestlength bestindex = curindex bestlength = curlength next note: can ditch line containing bestindex or curindex if don't care knowing subsequence occurs, seen in gary's implementation.

objective c - Reducing the memory footprint of a function with a lot of autoreleased variables? -

i'm still wrapping head around of nuances of memory management in objective-c, , came following case i'm unsure about: + (nsdecimalnumber*)factorial: (nsdecimalnumber *)l { nsdecimalnumber *index = l; nsdecimalnumber *running = [nsdecimalnumber one]; (; [index intvalue] > 1; index = [index decimalnumberbysubtracting:[nsdecimalnumber one]]) { running = [running decimalnumberbymultiplyingby: index]; } return running; } here decimalnumberbymultiplyingby , decimalnumberbysubtracting creating lot of nsdecimalnumbers, autoreleased understand it, worry until time containing program hanging unto awful lot of memory. should introducing autorelease pool somewhere? (if where?) going have noticeable effect on performance (when compared side effect of memory in use)? is autoreleasing right mechanism use here? should @ breaking loop apart , manually releasing memory i'm done it? it's n00b question, i'm trying flavour best practice...

will paginate - paginate_by_sql, next page error -

i'm using will_paginate (3.0.pre2) rails 3.0.0.rc. first page pulls fine when go page 2, returns nothing because query page 2 not being set correctly will_paginate. controller: page = params['page'].blank? ? 1 : params['page'] @posts = post.paginate_by_sql (['select * foo_table ids = ?', params[:ids]], :page=>page, :per_page=>50) @pag_params = {:ids=>params[:ids]} view: <%= will_paginate @posts, :params=>@pag_params %> link page 2 is: http://localhost:3000/posts/search?page=2&ids=1 logs show clause show incorrect (ids = 'search') when should (ids = '1') select * `foo_table` (ids = 'search') what missing query page 2 can corrected? talked developer of of will_paginate , found out best way attack using scoped_by_column_name(...column_value...).paginate(...conditions...) hope helps someone. :)

cryptography - CryptHashData equivalent for C#? -

i trying translate existing c++ code c#. have code calls crypthashdata 3 times on same hcrypthash . documentation says "this function , crypthashsessionkey can called multiple times compute hash of long or discontinuous data streams." which want achieve in c#. unfortunately, md5.computehash() doesn't appear have way build on existing hash. am missing something, there c# api achieve this? instantiate instance of hash class , use transformblock , transformfinalblock : byte[] part1 = //... byte[] part2 = //... byte[] part3 = //... var hash = new md5cryptoserviceprovider(); hash.transformblock(part1, 0, part1.length, null, 0); hash.transformblock(part2, 0, part2.length, null, 0); hash.transformfinalblock(part3, 0, part3.length); byte[] res = hash.hash;

database - Improve the speed of locating a row (integer columns) -

i have 15 integer column 5,000,000 rows in table. given input record containing 15 integers need compare input record 5,000,000 record table , obtain matching rows. note1: integers within row unique note2: order of columns matching , input record not important. example: 1, 10, 15, 23, 9, 22, 99, 11, 19, 32, 45, 21, 76, 12, 33 , 33, 10, 15, 99, 11, 19, 32, 45, 21, 23, 9, 22, 76, 12, 1 should yield match result is possible implement hashing function / bitwise operation generate unique index each row. function can return same index 2 rows if values in records same this isn't much, should started. you want hash function generates few collisions possible; has commutative (ie: order in add numbers hash irrelevant). can accomplish using combination of xor's , bit shifts (see page ). you might want store hash in column. can hash input looking , lookup hash on database. note hashes allow false positives, you'd still need check if candidate rows want (ie: sor...

apache - Handle HTTP POST with PHP -

i struggled half day , came conclusion can't done. threw away php scripts , rewrote in perl , worked right start way wanted work. still, want find out if such trivial task can done correctly in php. question: have arbitrarily long (in size , time) file upload (via raw data post) , need save file php. way works php fist processes posted data, saves file , execution of script begins (my file upload lasts 30 minutes). if tried fopen("php:/stdin" or php://input) still worked retarded way. need able process incoming posted data in chunks sequentially. tried: 1) modphp, 2) php-cgi, 3) php-cli run cgi executable. though php-cgi meant used cgi, still preprocesses posted data (so $_post becomes available) , doesn't work same way regular momphp. cli version run cgi script doens't work, can't read php://stdin or php://input @ all! whatever tried, nothing worked me , came conclusion can't done php... or can? thanks of course php can it. uploaded files s...