Ruby – Components may not be evil, but they sure can be slow
In : ruby, Posted by admin on Mar.03, 2009
http://railsexpress.de/blog/articles/2005/11/18/components-may-not-be-evil-but-they-sure-can-be-slow Becareful when you use Ruby components. When it goes back to the controller, it may run throught some filters and slow down the application.
Articles For Scaling Ruby On Rail Web Site
In : ruby, Posted by admin on Mar.03, 2009
Here are a set of articles for RoR scaling.
- The adventures of scaling, Stage 1
- Q&A: The adventures of scaling, Stage 1
- The adventures of scaling, Stage 2
- The adventures of scaling, Stage 3
- The adventures of scaling, Stage 4
- Scaling Rails with Apache 2.2, mod_proxy_balancer and Mongrel
Very good articles.
By the way, it said:
The old codebase roughly consisted of around 50.000 lines of PHP code (plus a closed-source CMS that’s (not included in this calculation). We’ve rewritten most of it (some features were left out on purpose) in about 5.000 lines of Rails code.
PPTwo Lines Client-Side JavaScript to Server-Side PHP
In : PHP, Posted by admin on Mar.03, 2009
It is a very useful helper function for PHP and Javascript.
http://phpolait.sourceforge.net/doc/index.html
PHP-O-Lait version 0.5.1 is released, with a bug fix that caused the jsolait.js JavaScript library not to be included correctly in certain circumstances. Many thanks to Olaf Bottek for locating the bug and assisting in correcting it. A few documentation changes have also been made, including an example of using PHP-O-Lait for asynchronous communication: thanks to Keith Powell for the idea.
Javascript for dynamic portal similar to netvibes
In : Uncategorized, Posted by admin on Mar.03, 2009
DHTML goodies has a downloadable javascript to build dynamic portal similar to http://www.netvibes.com and http://www.pageflakes.com.
The code is not very well organize. However, it is a start if you want to build a dynamic portal.
http://www.dhtmlgoodies.com/scripts/dragable-boxes/dragable-boxes.html
How can I insert a character entity like   into a document from Javascript ?
In : Uncategorized, Posted by admin on Mar.03, 2009
If you want to insert a non-breaking space you can insert it using the
charactor code. Like this:
var nbsp = document.createTextNode( "\u00A0" ); referenceToWhereYouWantIt.appendChild( nbsp );
The url above is to the complete ECMA Spec.
Dynamic Javascript Execution
In : Uncategorized, Posted by admin on Mar.03, 2009
A lot of time when you do an AJAX call, you want to dynamic execute the javascript within the AJAX updated region. For example:
<div id="ajax_update_here">
<!-- this is ajax return result -->
<script>
alert("this is a dynamic message from the ajax backend");
</script>
<!-- end of ajax return result -->
</div>
http://kratcode.wordpress.com/tag/javascript/ is a description of how to do it. Just call: execJS(‘ajax_update_here’); and it will execute the javascript within the div block.
function execJS(node)
{
var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
var bMoz = (navigator.appName == 'Netscape');
if (!node) return;
/* IE wants it uppercase */
var st = node.getElementsByTagName('SCRIPT');
var strExec;
for(var i=0;i<st.length; i++)
{
if (bSaf) {
strExec = st[i].innerHTML;
st[i].innerHTML = "";
} else if (bOpera) {
strExec = st[i].text;
st[i].text = "";
} else if (bMoz) {
strExec = st[i].textContent;
st[i].textContent = "";
} else {
strExec = st[i].text;
st[i].text = "";
}
try {
var x = document.createElement("script");
x.type = "text/javascript";
/* In IE we must use .text! */
if ((bSaf) || (bOpera) || (bMoz))
x.innerHTML = strExec;
else x.text = strExec;
document.getElementsByTagName("head")[0].appendChild(x);
} catch(e) {
alert(e);
}
}
};
Restart rails deploy in Dreamhost
In : ruby, Posted by admin on Mar.03, 2009
If you are using Dreamhost to deploy Ruby On Rails, I suggest you to look into:
Webistrano – http://labs.peritor.com/webistrano
This is a web UI to make PHP, rails deploy a lot easier. 5 stars!
Also, in case you change the source code in Dreamhost and want Dreamhost to reload the file, you see to do the following:
- SSH to your Dreamhost account
- echo “” > YOUR_DEPLOY_DIR_NAME/tmp/restart.txt
- This is trigger dreamhost’s Passenger to restart your files. Also, at the end, it will delete the tmp/restart.txt file.
How to do javascript subclassing
In : Uncategorized, linux, mysql, Posted by admin on Mar.03, 2009
http://www.golimojo.com/etc/js-subclass.html
function subclass(constructor, superConstructor)
{
function surrogateConstructor()
{
}
surrogateConstructor.prototype = superConstructor.prototype;
var prototypeObject = new surrogateConstructor();
prototypeObject.constructor = constructor;
constructor.prototype = prototypeObject;
}
How to sort data in a big file
In : Uncategorized, Posted by admin on Mar.03, 2009
This is from wikipedia (http://en.wikipedia.org/wiki/External_sorting)
One example of external sorting is the external mergesort algorithm. For example, for sorting 900 megabytes of data using only 100 megabytes of RAM:
1. Read 100 MB of the data in main memory and sort by some conventional method (usually quicksort).
2. Write the sorted data to disk.
3. Repeat steps 1 and 2 until all of the data is sorted in 100 MB chunks, which now need to be merged into one single output file.
4. Read the first 10 MB of each sorted chunk (call them input buffers) in main memory (90 MB total) and allocate the remaining 10 MB for output buffer.
5. Perform a 9-way merging and store the result in the output buffer. If the output buffer is full, write it to the final sorted file. If any of the 9 input buffers gets empty, fill it with the next 10 MB of its associated 100 MB sorted chunk or otherwise mark it as exhausted if there is no more data in the sorted chunk and do not use it for merging.
This algorithm can be generalized by assuming that the amount of data to be sorted exceeds the available memory by a factor of K. Then, K chunks of data need to be sorted and a K-way merge has to be completed. If X is the amount of main memory available, there will be K input buffers and 1 output buffer of size X/(K+1) each. Depending on various factors (how fast the hard drive is, what is the value of K) better performance can be achieved if the output buffer is made larger (for example twice as large as one input buffer).
In the example, a single-pass merge was used. If the ratio of data to available main memory is particularly large, a multi-pass sorting is preferable. For example, merge only the first half of the sorted chunks, then the other half and now the problem has been reduced to merging just two sorted chunks. The exact number of passes depends on the above mentioned ratio, as well as the physical characteristics of the hard drive (transfer rate and seeking time). As a rule of thumb, it is inadvisable to perform a more-than-20-to-30-way merge.
Another silly interview question about how to swap 2 variables without temporary variable
In : Uncategorized, Posted by admin on Mar.03, 2009
How to swap 2 variables without using a temporary variable. Silly…
a=a+b;
b=a-b;
a=a-b;