Category : Uncategorized
Guide: Things You Shouldn’t Be Doing In Rails
In : Uncategorized, Posted by admin on Mar.03, 2009
Good read. But it is not easy to stop using some of this stuffs (like scaffolding, authentication engine, pagination). They are so easy to use and this is why people come to Ruby in the first place.
http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails
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);
}
}
};
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;
CSS box model
In : Uncategorized, Posted by admin on Mar.03, 2009
Each box has a content area (e.g., text, an image, etc.) and optional surrounding padding, border, and margin areas; the size of each area is specified by properties defined below. The following diagram shows how these areas relate and the terminology used to refer to pieces of margin, border, and padding:
The margin, border, and padding can be broken down into left, right, top, and bottom segments (e.g., in the diagram, “LM” for left margin, “RP” for right padding, “TB” for top border, etc.).
The perimeter of each of the four areas (content, padding, border, and margin) is called an “edge”, so each box has four edges:
- content edge or inner edge
- The content edge surrounds the element’s rendered content.
- padding edge
- The padding edge surrounds the box padding. If the padding has 0 width, the padding edge is the same as the content edge. The padding edge of a box defines the edges of the containing block established by the box.
- border edge
- The border edge surrounds the box’s border. If the border has 0 width, the border edge is the same as the padding edge.
- margin edge or outer edge
- The margin edge surrounds the box margin. If the margin has 0 width, the margin edge is the same as the border edge.
Each edge may be broken down into a left, right, top, and bottom edge.
The dimensions of the content area of a box — the content width and content height — depend on several factors: whether the element generating the box has the ‘width’ or ‘height’ property set, whether the box contains text or other boxes, whether the box is a table, etc. Box widths and heights are discussed in the chapter on visual formatting model details.
The box width is given by the sum of the left and right margins, border, and padding, and the content width. The height is given by the sum of the top and bottom margins, border, and padding, and the content height.
The background style of the various areas of a box are determined as follows:
- Content area: The ‘background’ property of the generating element.
- Padding area: The ‘background’ property of the generating element.
- Border area: The border properties of the generating element.
- Margin area: Margins are always transparent.
Problem with IE css hack
According to the W3C, an assigned ‘width’ (and ‘height’) of a box refers to the ‘content area’ of a box only. The padding, borders, and margins are then added to this value to arrive at the total box width. If the ‘width’ property is omitted, the total box width is the same as the ‘content area’ of the surrounding container element.
All well and good. Unfortunately, all CSS enabled versions of IE before IE6/strict use a different box model. In that model, the padding and borders are counted as part of any assigned ‘width’ or ‘height’. In the absence of borders and padding, the two models agree. However, if a box has an assigned “width’, and if borders and/or padding are added, the standard box model causes the overall box width (between the outer border edges) to increase, while in IE’s model the ‘content area’ gets squeezed by the same amount. This is a major problem for proper page layout.
Consider the following CSS:
- {width:100px; padding:10px; border:10px;}
When viewed in a ’standards’ browser the dimension from border edge to border edge will be ‘140px’. (100+10+10+10+10=140) Because IE5.x puts all these values inside the ‘width’, the border edge to border edge dimension will be ‘100px’.
Note: For technical reasons it sometimes would be desirable to employ the old IE box model. It has been bruited about that CSS-3 will feature a way to choose between the two models, but the current standard model will no doubt remain the default.
Javascript closure
In : Uncategorized, Posted by admin on Mar.03, 2009
A javascript closure is the local variables for a function – kept alive after the function has returned. For example, in the following example, the variable text is still alive after the function is closed. The reason is that the internal function sayAlert is still alive.
function sayHello2(name) {
var text = 'Hello ' + name; // local variable
var sayAlert = function() { alert(text); }
return sayAlert;
}
Source: http://blog.morrisjohns.com/javascript_closures_for_dummies
How to setup MyDNS
In : Uncategorized, Posted by admin on Nov.11, 2008
Useful document for how to setup MyDNS. The database table name may need to change but in general very good.
