I found a link to how to inline swap using mktemp and a shell function for Ubuntu, but Mac OS X has a slightly different version of mktemp, which requires an “template.XXXXX” to be specified, where “template.” is template text that will always be included, and XXXXX (of whatever length) will have random characters.
I decided to time test a few variations of Fibonacci sequence implementation found on StackOverflow. The hash implementation seems incredibly fast, but will generate a stack overflow if trying to generate a sequence member too far (10000 elements for me) from previously accessed values.
The lambda version becomes unusable at about 40 elements for me, with the run times mimicking a Fibonnaci sequence themselves.
1234567891011121314151617181920212223
# lambda definitionf=->x{x<2?x:f[x-1]+f[x-2]}# f[0] takes 0.009 milliseconds, f[20] took 21 ms, f[29] took 326 ms:20.timesdo|x|duration=time_block{f[x]}puts"f[#{x}] took #{duration} milliseconds"end# even access times, because previous loops cache the h[k-1] and h[k-2] valueshash_fib=Hash.new{|h,k|h[k]=k<2?k:h[k-1]+h[k-2]}20.timesdo|x|duration=time_block{hash_fib[x]}puts"hash_fib[#{x}] took #{duration} milliseconds"endhash_fib.clear# first hash_fib[2000] call takes 9.368 ms, subsequent calls take hash access time (0.001 ms)20.timesdo|x|duration=time_block{hash_fib[2000]}puts"hash_fib[#{2000}] took #{duration} milliseconds"end
I created a simple rspec test for a very basic controller that sent my rspec tests into a memory-sucking tailspin:
123456
describe"GET 'index'"doit"should contain this basic text"doget'index'response.should=~/regex to test/endend
Amazingly, this code will result in ruby consuming every byte of memory the OS will allow it to have. The code for “response.should” should be
1
response.body.should=~/regex to test/
I verified that doing a “.should =~” on TestResponse would cause this problem via rails console. The following lines entered into rails console cause ruby to start consuming memory:
I have a underlying session timeout being tracked in rails and I want to make sure that the client interface doesn’t get ahead of that.
First, we create a function, set_expire, to establish when the expire timeout will occur. Then, we establish a check_timeout function to check if the expire time is passed and redirect to a login page. If set_expire is called again, the expiration time will be moved based on the current time.
These functions will be placed in the static application.js file that we be loaded on every full page load.
Next, we initiate the loading of the timeout polling by calling set_expire on document.ready and initiating the polling interval with window.setInterval.
Finally, we create a hook for extending the timeout on ajax calls via the jQuery function .ajaxComplete
I’m fairly new to Rails development in general, so this is a pretty rudimentary task, but I wanted to bundle several tasks into one to quickly prep a demo environment without much intervention (or thought).
I wanted my task to be setup:demo, so I added a setup file called in lib/tasks, called “setup.rake”. The invoke method was used to due to its honoring dependencies.
Bundling this set of tasks together is probably going to make experienced Rails developers cringe, but I’m just packaging it together because I end up running this setup independently anyway.
12345678910111213141516
namespace:setupdodesc"Stage the system for a demo"task:demodosteps=%w(db:drop db:create db:migrate db:seed)steps.eachdo|step|puts"Invoking Rake::Task[\"#{step}\"]"Rake::Task[step].invokeend# the following model needs to be reloaded prior to doing# the next two tasksUser.reset_column_informationRake::Task["admin:generate"].invokeRake::Task["users:generate"].invokeendend