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

                    
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 */
                
                    If WEBrick is leaking out of control, which it does do all the time, shut it down and restart it. 
Ctrl-c + Up + Enter
WEBrick can slow your computer down to a crawl so it is reccomended that you only use it when it's needed. Otherwise just runs tests, WEBrick's leaks are just another great reason to adapt to test driven development.                
                    one-liner to print out the current date & time given the time()

perl -le 'print scalar localtime shift'
                
                    <!--
*** str_repeat
*** Returns str repeated multiplier times
*** Param str = the string
*** Param multiplier = the string
-->
-
	<xsl:stylesheet version="1.0">
-
	<xsl:template match="/">
<xsl:param name="string" select="'Hello'"/>
-
	<xsl:call-template name="str_repeat">
<xsl:with-param name="str" select="$string"/>
<xsl:with-param name="multiplier" select="number(10)"/>
</xsl:call-template>
</xsl:template>
-
	<xsl:template name="str_repeat">
<xsl:param name="str"/>
<xsl:param name="multiplier"/>
<xsl:param name="count" select="number(1)"/>
-
	<xsl:if test="$count <= $multiplier">
-
	<xsl:call-template name="str_repeat">
<xsl:with-param name="str" select="$str"/>
<xsl:with-param name="multiplier" select="$multiplier"/>
<xsl:with-param name="count" select="$count+1"/>
</xsl:call-template>
<xsl:value-of select="$str"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>                
                    <!--
*** ucfirst
*** Make a string's first character uppercase
*** Param str = the string
-->
-
	<xsl:stylesheet version="1.0">
-
	<xsl:template match="/">
<xsl:param name="string" select="'the quick brown fox jumped over the lazy dog.'"/>
-
	<xsl:call-template name="ucfirst">
<xsl:with-param name="str" select="$string"/>
</xsl:call-template>
</xsl:template>
-
	<xsl:template name="ucfirst">
<xsl:param name="str"/>
<xsl:param name="strLen" select="string-length($str)"/>
<xsl:variable name="firstLetter" select="substring($str,1,1)"/>
<xsl:variable name="restString" select="substring($str,2,$strLen)"/>
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="translate" select="translate($firstLetter,$lower,$upper)"/>
<xsl:value-of select="concat($translate,$restString)"/>
</xsl:template>
</xsl:stylesheet>                
                    def foobar(astring, bstring)
  Hash.new do |l,(i, j)|
    if l.key? [i, j]
      l[[i, j]]
    else
      a, b = astring[i], bstring[j]
      l[[i, j]] = case
      when !a, !b then  0
      when a == b then  1 + l[[i + 1, j + 1]]
      else              [ l[[i + 1, j]], l[[i, j + 1]] ].max
      end
    end
  end[[0, 0]]
end