Never been to DZone Snippets before?

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

About this user

« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS 

no www

   1  
   2  RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
   3  RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

SSH Key Pairs Stuff

//allows for copying of ssh keys

   1  
   2  ssh-keygen -t dsa
   3  ssh user@host 'cat >> .ssh/authorizedkeys' < .ssh/id_dsa.pub

Easy default options for methods in Ruby

   1  
   2  def my_method(opts={})
   3    {:arg_one => 'foo', :arg_two => 'two'}.merge(opts)
   4  end

Tag Cloud in Ruby

The options.delete with the default looks mildly interesting. Just wanted to remember that.

   1  
   2  def font_size_for_tag_cloud( total, lowest, highest, options={} )
   3   return nil if total.nil? or highest.nil? or lowest.nil?
   4   #
   5   # options
   6   maxf = options.delete( :max_font_size ) || 14
   7   minf = options.delete( :min_font_size ) || 11
   8   maxc = options.delete( :max_color ) || [ 0, 0, 0 ]
   9   minc = options.delete( :min_color ) || [ 156, 156, 156 ]
  10   hide_sizes = options.delete( :hide_sizes )
  11   hide_colours = options.delete( :hide_colours )
  12   #
  13   # function to work out rgb values
  14   def rgb_color( a, b, i, x)
  15    return nil if i <= 1 or x <= 1
  16    if a > b
  17     a-(Math.log(i)*(a-b)/Math.log(x)).floor
  18    else
  19     (Math.log(i)*(b-a)/Math.log(x)+a).floor
  20    end
  21   end
  22   #
  23   # work out colours
  24   c = []
  25   (0..2).each { |i| c << rgb_color( minc[i], maxc[i], total, highest ) || nil }
  26   colors = c.compact.empty? ? minc.join(',') : c.join(',')
  27   #
  28   # work out the font size
  29   spread = highest.to_f - lowest.to_f
  30   spread = 1.to_f if spread <= 0
  31   fontspread = maxf.to_f - minf.to_f
  32   fontstep = spread / fontspread
  33   size = ( minf + ( total.to_f / fontstep ) ).to_i
  34   size = maxf if size > maxf
  35   #
  36   # display the results
  37   size_txt = "font-size:#{ size.to_s }px;" unless hide_sizes
  38   color_txt = "color:rgb(#{ colors });" unless hide_colours
  39   return [ size_txt, color_txt ].join
  40  end

pop2imap

// description of your code here

   1  
   2  pop2imap --host1 westk.org --user1 rod@westk.org --password1 password --host2 orderedserver.com --user2 rod@addictedtonew.com --password2 password 

Easy Selects in Ruby on Rails

Via http://habtm.com/articles/2006/04/10/handy-select-functions

   1  
   2  module ActiveRecord
   3    class Base  
   4      def self.to_select(conditions = nil)
   5        find(:all).collect { |x| [x.name,x.id] }
   6      end
   7    end
   8  end
   9  
  10  class Array
  11    def to_select
  12      self.collect { |x| [x.name,x.id] }
  13    end
  14  end

Restart apache on Mac OS X

   1  
   2  sudo apachectl restart

Change iChat status from terminal.app

   1  
   2  osascript ichatloc.scpt "This is a test"

Add MySQL User

# add a mysql user (http://dev.mysql.com/doc/refman/5.0/en/grant.html)
   1  
   2  grant all privileges on *.* to 'user'@'localhost' identified by 'password' with grant option;
   3  flush priveleges;

Creating a PDF using iText and ColdFusion

// description of your code here

   1  
   2  <cfscript>
   3  // create document
   4  document 		= CreateObject("java", "com.lowagie.text.Document");
   5  document.init();
   6  
   7  // writer
   8  fileIO 			= CreateObject("java", "java.io.FileOutputStream");
   9  fileIO.init(pdf_path);
  10  writer 			= CreateObject("java", "com.lowagie.text.pdf.PdfWriter");
  11  writer.getInstance(document, fileIO);
  12  document.open();
  13  
  14  // newsinfo header image
  15  Image 			= CreateObject("java", "com.lowagie.text.Image");
  16  jpg 			= Image.getInstance(header_image);
  17  jpg.setAbsolutePosition(28, 713);
  18  jpg.setDpi(300,300);
  19  document.add(jpg);
  20  
  21  // top margin; dumb i know but i was in a hurry
  22  paragraph = CreateObject("java", "com.lowagie.text.Paragraph");
  23  paragraph.init(" ");
  24  for (i=0; i lt 9; i=i+1) {
  25  	document.add(paragraph);
  26  }
  27  
  28  // the fonts
  29  FontFactory 	= createobject("java", "com.lowagie.text.FontFactory");
  30  Font 			= createObject("java", "com.lowagie.text.Font");
  31  TimesLargeBI 	= Font.init(Font.TIMES_ROMAN, 14.0, Font.BOLDITALIC);
  32  TimesNormal 	= Font.init(Font.TIMES_ROMAN, 12.0);
  33  
  34  // all the text
  35  paragraph 		= CreateObject("java", "com.lowagie.text.Paragraph");
  36  
  37  paragraph.init("Hello World!", TimesLargeBI);
  38  paragraph.setIndentationLeft(indentation_left);
  39  paragraph.setIndentationRight(indentation_right);
  40  document.add(paragraph);
  41  
  42  paragraph.init("#dateFormat(now(), 'long')#", TimesNormal);
  43  paragraph.setIndentationLeft(indentation_left);
  44  paragraph.setIndentationRight(indentation_right);
  45  document.add(paragraph);
  46  
  47  document.close();
  48  </cfscript> 
« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS