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

Zeke Sikelianos

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

Ruby method to determine if a string would look more readable on black vs. white.

// description of your code here

   1  
   2  def self.ideal_font_color_for(hex_str)
   3  	hex_str = hex_str.split('#').last
   4  
   5  	threshold = 105
   6  
   7  	r = hex_str[0..1].hex
   8  	g = hex_str[2..3].hex
   9  	b = hex_str[4..5].hex
  10  
  11  	delta = (r*0.299) + (g*0.587) + (b*0.114)
  12  
  13  	255 - delta < threshold ? "#000000" : "#FFFFFF"
  14  end

Sort last 1000 shell commands by popularity

   1  
   2    history 1000 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head

Bashy Perlness for generating Favicon text for OpenSearches

   1  
   2  echo -n '<Image width="16" height="16">data:image/xicon,' ; perl -ne 's/(.)/"%".unpack("H2",$1)/egs; print' ~/Desktop/favicon.ico ; echo '</Image>' 

Ruby: Escape, Unescape, Encode, Decode, HTML, XML, URI, URL

This example will show you how to escape and un-escape a value to be included in a URI and within HTML.

   1  
   2  require 'cgi'
   3  
   4  # escape
   5  name = "ruby?"
   6  value = "yes"
   7  url = "http://example.com/?" + CGI.escape(name) + '=' + CGI.escape(value) + "&var=T"
   8  # url: http://example.com/?ruby%3F=yes&var=T
   9  html = %(<a href="#{CGI.escapeHTML(url)}">example</a>)
  10  # html: <a href="http://example.com/?ruby%3F=yes&amp;var=T">example</a>
  11  
  12  # unescape
  13  name_encoded = html.match(/http:([^"]+)/)[0]
  14  # name_encoded: http://example.com/?ruby%3F=yes&amp;var=T
  15  href = CGI.unescapeHTML(name_encoded)
  16  # href: http://example.com/?ruby%3F=yes&var=T
  17  query = href.match(/\?(.*)$/)[1]
  18  # query: ruby%3F=yes&var=T
  19  pairs = query.split('&')
  20  # pairs: ["ruby%3F=yes", "var=T"]
  21  name, value = pairs[0].split('=').map{|v| CGI.unescape(v)}
  22  # name, value: ["ruby?", "yes"]
  23  

Single-Line commenting in Actionscript with Textmate

- open the bundle editor and select the Actionscript bundle
- use the add button to make a new preference item, give it a scope of source.actionscript
- name it whatever
- paste in the following:

   1  
   2  {   shellVariables = (
   3          {   name = 'TM_COMMENT_START';
   4              value = '// ';
   5          },
   6      );
   7  }


- that's it! got these instructions on IRC from Infininight: http://pastie.textmate.org/private/clmfldbv2sexjcd7u6qjw

AS3 FlashVars equivalent: LoaderInfo

// Add this to your package..
import flash.text.*;

// And throw this in wherever..
var t:TextField = new TextField();
t.autoSize = TextFieldAutoSize.LEFT;
t.border = true;
addChild(t);

t.appendText("params:" + "\n");
try {
var key:String;
var val:String;
var flashvars:Object = LoaderInfo(this.root.loaderInfo).parameters;
for (key in flashvars) {
val = String(flashvars[key]);
t.appendText("\t" + key + ":\t" + val + "\n");
}
} catch (error:Error) {
t.appendText(error);
}

Make AS3 clips turn cursor into a hand on rollover..

   1  
   2    buttonMode = true; 
   3    mouseChildren = false;

AS3 SwapDepths Equivalent

   1  
   2  function move_to_top() {
   3    // This will move the current object to the topmost z-index
   4    parent.setChildIndex(this, parent.numChildren-1);
   5  }

Ruby word count

   1  
   2  module StringExtensions
   3    def words
   4      s = self.dup
   5      s.gsub!(/\w+/, 'X')
   6      s.gsub!(/\W+/, '')
   7      s.length
   8    end
   9  end

Find all subdirectories of a given path at a particular depth

   1  
   2  def subdirectories_of(path, options = {}) depth = options[:at_depth_of] || 1 Dir[File.join(path, * ["*"] * depth + [""])] end >> subdirectories_of("/var") => ["/var/agentx/", "/var/amavis/", "/var/at/", "/var/audit/", "/var/backups/", "/var/db/", "/var/empty/", "/var/folders/", ...] >> subdirectories_of("/var", :at_depth_of => 2) => ["/var/amavis/db/", "/var/amavis/tmp/", "/var/at/jobs/", "/var/at/spool/", "/var/at/tabs/", "/var/at/tmp/", 
« Newer Snippets
Older Snippets »
Showing 1-10 of 66 total  RSS