DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets

  • submit to reddit

Recent Snippets

                    
if (req.readyState == 4) {

	if (req.status == 200) {
		
            // ...processing statements go here...

            response = req.responseXML;
            
            results = response.getElementsByTagName(name);

	}
}
                
                    in Firefox

user_pref("javascript.options.strict", true);
console display 'warning'                
                    Assume you have copied a file across the network using something like this:

scp host:/path/to/file .

Then assume something goes wrong, so the transfer is interrupted. To continue the transfer, do this:

rsync --partial host:/path/to/file .

(Or, rather, always use rsync as above when transferring very large files.)

(Also note: I don't know how frequently rsync flushes its buffer, but don't assume something is wrong just because the file you're downloading doesn't increase in size in the file system. When you interrupt rsync using Ctrl-C, it will flush its buffers.)                
                    

begin
   Thread.current.kill
   exit(1)
   #`kill -9 #{$$}` # this will successfully kill you
   puts "after kill"
ensure
   puts "HA HA!"
end


always interesting to play with this kind of code.
and discover how much ensure means ENSURE !!!!

Ruby: Great Language.

                
                    
class DefaultController < ApplicationController
    def default
        result = Net::HTTP.get_response(
            "realserver.internal.net",
            @request.env["REQUEST_URI"]
            )
        #render error if result. ...
        render_text result.body
        end
    end
                
                    This function takes a time and returns a relative date string (it does, however, ignore the time of day). For instance: "yesterday", "in 2 days", "1 day ago", "March 12", or "May 5, 1992".

function relative_date($time) {
    $today = strtotime(date('M j, Y'));
    $reldays = ($time - $today)/86400;
    if ($reldays >= 0 && $reldays < 1) {
        return 'today';
    } else if ($reldays >= 1 && $reldays < 2) {
        return 'tomorrow';
    } else if ($reldays >= -1 && $reldays < 0) {
        return 'yesterday';
    }
    if (abs($reldays) < 7) {
        if ($reldays > 0) {
            $reldays = floor($reldays);
            return 'in ' . $reldays . ' day' . ($reldays != 1 ? 's' : '');
        } else {
            $reldays = abs(floor($reldays));
            return $reldays . ' day'  . ($reldays != 1 ? 's' : '') . ' ago';
        }
    }
    if (abs($reldays) < 182) {
        return date('l, F j',$time ? $time : time());
    } else {
        return date('l, F j, Y',$time ? $time : time());
    }
}
                
                    Add to .vimrc so you can un comment blocks of text quickly and easily.

" ,cc clear the comments
map ,cc :s/^\/\/\\|^--\\|^> \\|^[#"%!;]//<CR><Esc>:nohlsearch<CR>
                
                    Add to .vimrc so you can comment blocks of text in perl/java/etc.

Highlight a block of text and hit ,# and it is commented out in perl.

" , #perl # comments
map ,# :s/^/#/<CR>

" ,/ C/C++/C#/Java // comments
map ,/ :s/^/\/\//<CR>

" ,< HTML comment
map ,< :s/^\(.*\)$/<!-- \1 -->/<CR><Esc>:nohlsearch<CR>

" c++ java style comments
map ,* :s/^\(.*\)$/\/\* \1 \*\//<CR><Esc>:nohlsearch<CR>
                
                    >>> import os

>>> os.getcwd()     # get current working directory
'C:\\lang\\python23'

>>> os.chdir('C:/')  # change directory
>>> os.chdir('lang')  # again

>>> os.listdir('.')  # list
['dotnet', 'try', 'editplus', 'python23', 'leo', 'mingw', 'perl', 'gtk', 'ironpy', 'java142']
>>>                 
                    
# dpkg --force-depends --purge exim4-daemon-light
# dpkg -i qmail_1.03-36_i386.deb