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

                    
things = [] #[1,2,3]

if things.each do |thing|
    puts thing
end.empty? then
    puts "no things!"
end
                
                    Example of a Auto Attendant menu.

[MainMenu]
exten => s,1,SetVar(NUMINVALID=0)
exten => s,2,Setvar(NUMTIMEOUTS=0)
exten => s,3,Background(sales_press_one)
exten => s,4,Background(support_press_two))
exten => s,5,ResponseTimeout(5)
exten => s,6,DigitTimeout(2)

exten => t,1,SetVar(NUMTIMEOUTS=$[${NUMTIMEOUTS} + 1])
exten => t,2,Gotoif($[${NUMTRIES} < 2]?s,3)
exten => t,3,Background(vm-goodbye)
exten => t,4,Hangup()

exten => i,1,SetVar(NUMINVALID=$[${NUMINVALID} + 1])
exten => i,2,Gotoif($[${NUMINVALID} < 4]?:10)
exten => i,3,Background(invalid)
exten => i,4,Goto(s,2)
exten => i,10,Playback(vm-goodbye)
exten => i,11,Hangup()
                
                    
sudo port install ruby
sudo port install rb-rubygems
sudo gem install rails --include-dependencies
sudo ln /opt/local/bin/ruby /usr/local/bin/ruby

Originally from http://labs.broqn.com/blog/2005/03/ruby-on-rails-with-darwinports/ (added --include-dependencies)                
                    
cvs -d :pserver:anonymous@anoncvs.opendarwin.org:/Volumes/src/cvs/od login
cvs -d :pserver:anonymous@anoncvs.opendarwin.org:/Volumes/src/cvs/od update -P darwinports

or, if you didn't get the source from CVS:

sudo port -d selfupdate
                
                    if you do not already have a key pair
$ ssh-keygen -t dsa

send your public key to each host you ssh into
$ cat ~/.ssh/id_dsa.pub | ssh you@host 'cat - >> ~/.ssh/authorized_keys2'

now you can use ssh-agent and ssh-add to facilitate a "single signon" situation.

useful if you're ssh'ing into a bunch of machines all day.

google ssh-agent for distribution-specific details.                
                    
File.dirname "http://foo.com/bar/stuff.html"
#=> "http://foo.com/bar"

File.basename "http://foo.com/bar/stuff.html"
#=> "stuff.html"

File.split "http://foo.com/bar/stuff.html"
#=> ["http://foo.com/bar", "stuff.html"]

File.join( File.dirname("http://foo.com/bar/doc.html"), "relative_link.html" )
# => "http://foo.com/bar/relative_link.html"

(Originally by Ilmari Heikkinen on the ruby-talk mailing list.)                
                    In your controller:

  layout :my_layout_func

  ...

protected

  def my_layout_func
   # anything ruby will work
   ['list','index',nil].include?(action_name)? 
   'noheading':'my_layout'
 
   # or you could do
   case action_name
    when 'monkey': 'monkey_layout'
    when 'edit'  : 'form_layout'
   end
 end


                 
                    careful who gets access to this script. as far as i can tell this can only modify files in its own directory (although i didn't explicitly make it that way). im new at perl so careful anyway. questions or comments? (dj_bidi@fastmail.fm)

#!/usr/bin/perl

##         SLEd (Sort Of Like Ed) Editor
## USE:
##  append commands to url after a question mark 
## COMMANDS:
##  open (temp) file:                      -f,[file_to_edit]
##  close (temp)file:                      -d 
##  delete real file (careful!):           -dd
##  insert line (0 inserts at beginning):  -[line_number]a,[new_text] 
##  replace line:                          -[line_number],[new_text]
##  regex replace in line (like s///g):    -[line_number],/[old]/[new]/
##  add line:                              [line_to_add]
##  delete line:                           -[line_number]
##  save over real file:                   . 
##  save as new file:                      .[newfile_name]
## QUIRKS:
##  use ":lb:" to pass a #

$in = $ENV{'QUERY_STRING'};
$in =~ s/%3C/</g;
$in =~ s/%3E/>/g;
$in =~ s/:lb:/#/g;
$in =~ s/%20/ /g;
$in =~ s/%22/"/g;

$url = $ENV{'SCRIPT_URI'};
($name = $url) =~ s/.+\///;
$file = "~SLEd.pl";

if ($in =~ /-f,(.*)/) {
 if ($file ne "") {
  unlink "$file";
 }
 $new = $1;
 $new =~ s/;//g;
 $new =~ s/"//g;
 open (IN, "$new");
 if (length($new) != 0) { 
  open (OUT, ">~$new");
 }
 while ($_ = <IN>) {
  print OUT "$_";
 }
 close IN;
 close OUT;
 rename "$name", "~bak";
 open (IN, "~bak");
 open (OUT, ">$name");
 while ($_ = <IN>) {
  if (length($new) == 0) {
   $_ =~ s/\$file = \".*\"/\$file = \"\"/g;
  } else {
   $_ =~ s/\$file = \".*\"/\$file = \"~$new\"/g;
  } 
  print OUT "$_";
 }
 close IN;
 close OUT;
 unlink "~bak";
 print "location: $url\n\n";
 die;
}

if ($in  eq ".") {
 ($f = $file) =~ s/^~//;
 unlink "$f";
 rename("$file", "$f");
 print "location: $url?-f,\n\n";
 die;
} 

if ($in eq "-dd") {
 ($f = $file) =~ s/^~//;
 unlink "$f";
 unlink "$file";
 print "location: $url?-f,\n\n";
 die;
} 

if ($in =~ /^\.(.+)/) {
 $new = $1;
 $new =~ s/;//g;
 $new =~ s/"//g;
 ($f = $file) =~ s/^~//;
 rename("$file", "$new");
 print "location: $url?-f,\n\n";
 die;
} 

if ($in eq "-d") {
 unlink "$file";
 print "location: $url?-f,\n\n";
 die;
} 

if ($in =~ /^-([0-9]+)a,(.+)/) {
  $i = 1;
  if ($1 == 0) {
   push(@print, "$2\n");
  }
  open (F, "$file");
  while (<F>) {
   if ($i != $1) {
    push(@print, $_);
   } else {
    push(@print, $_);
    push(@print, "$2\n");
   }
   $i++;
  }
  close F; 
  open (F, ">$file");
  foreach $line(@print) {
   print F "$line";
  }
  close F;
  print "location: $url\n\n";

} elsif ($in =~ /^-([0-9]+),(.+)/) {
  $lino = $1;
  $replace = $2;
  $i = 1;
  open (F, "$file");
  while (<F>) {
   if ($i != $lino) {
    push(@print, $_);
   } elsif ($replace =~ m/^\/(.+?)\/(.*)\/$/) {
    $ol = $1;
    $newe = $2;
    ($match = $_) =~ s/$ol/$newe/g;
    push(@print, $match);
   } else {
    push(@print, "$replace\n");
   }
   $i++;
  }
  close F;  
  open (F, ">$file");
  foreach $line(@print) {
   print F "$line";
  }
  close F;
  print "location: $url\n\n";

} elsif ($in =~ /^-([0-9]+)$/) {
 $i = 1;
 open (F, "$file");
 while (<F>) {
  if ($i != $1) {
   push(@print, $_);
  }
  $i++;
 }
 close F;
 open (F, ">$file");
 foreach $line(@print) {
  print F "$line";
 }
 close F;
 print "location: $url\n\n";

} elsif ($in) {
 open (F, ">>$file");
 print F "$in\n";
 close F;
 print "location: $url\n\n";

} else {
 print "Content-type: text/plain\n\n";
 open (F, "$file");
 $i = 1;
 while (<F>) {
  print "[$i] $_";
  $i++;
 }
}
                
                    
javascript:(function(){var s=document.createElement("script");s.charset="UTF-8";s.src="http://example.com/example.js";document.body.appendChild(s)})();
It's cool!!!                
                    Float clearing CSS...

.container:after {
    content: "."; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;
}

.container {display: inline-table;}

/* Hides from IE-mac \*/
* html .container {height: 1%;}
.container {display: block;}
/* End hide from IE-mac */