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

def self.ideal_font_color_for(hex_str)
	hex_str = hex_str.split('#').last

	threshold = 105

	r = hex_str[0..1].hex
	g = hex_str[2..3].hex
	b = hex_str[4..5].hex

	delta = (r*0.299) + (g*0.587) + (b*0.114)

	255 - delta < threshold ? "#000000" : "#FFFFFF"
end

Sort last 1000 shell commands by popularity

  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

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.

require 'cgi'

# escape
name = "ruby?"
value = "yes"
url = "http://example.com/?" + CGI.escape(name) + '=' + CGI.escape(value) + "&var=T"
# url: http://example.com/?ruby%3F=yes&var=T
html = %(<a href="#{CGI.escapeHTML(url)}">example</a>)
# html: <a href="http://example.com/?ruby%3F=yes&amp;var=T">example</a>

# unescape
name_encoded = html.match(/http:([^"]+)/)[0]
# name_encoded: http://example.com/?ruby%3F=yes&amp;var=T
href = CGI.unescapeHTML(name_encoded)
# href: http://example.com/?ruby%3F=yes&var=T
query = href.match(/\?(.*)$/)[1]
# query: ruby%3F=yes&var=T
pairs = query.split('&')
# pairs: ["ruby%3F=yes", "var=T"]
name, value = pairs[0].split('=').map{|v| CGI.unescape(v)}
# name, value: ["ruby?", "yes"]

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:

{   shellVariables = (
        {   name = 'TM_COMMENT_START';
            value = '// ';
        },
    );
}


- 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..

  buttonMode = true; 
  mouseChildren = false;

AS3 SwapDepths Equivalent

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

Ruby word count

module StringExtensions
  def words
    s = self.dup
    s.gsub!(/\w+/, 'X')
    s.gsub!(/\W+/, '')
    s.length
  end
end

Find all subdirectories of a given path at a particular depth

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