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 11-20 of 66 total

Rails validation for Phone

  validates_length_of :phone, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.phone.blank?}
  validates_length_of :fax, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.fax.blank?}

Ruby: Open a file, write to it, and close it in one line

r - Open a file for reading. The file must exist.
w - Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
a - Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
r+ - Open a file for update both reading and writing. The file must exist.
w+ - Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.
a+ - Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

File.open(local_filename, 'w') {|f| f.write(doc) }

MySQL Dump entire database

mysqldump -v --quick --single-transaction --all-databases -uroot > file.sql

Rename old HTML views for Rails 2

# Snagged from http://softiesonrails.com/2007/7/11/upgrading-your-views-to-rails-2-0
for old in `find app/views -name *.rhtml`; do svn mv $old `dirname $old`/`basename $old .rhtml`.html.erb; done

PHP for removing www from request and redirecting. (Useful for System.security.allowDomain issues)

<?
	// if www.domain.com, redirect to domain.com
	if (strtolower(substr($_SERVER['HTTP_HOST'], 0, 3)) == "www") {
		header("Location: http://rgcreative.com" . $_SERVER['REQUEST_URI']);
	}
	
	// Full path to current URL (including query string)
	//echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
?>

Less known ruby tricks (?): neat "sprintf" shortcut

// description of your code here

>> "%.2f" % 45.4324234321421
=> "45.43"

>> "[%s]" % "Hey you dirty old ruby, put me in brackets. NOW!"
=> "[Hey you dirty old ruby, put me in brackets. NOW!]"

For more complex substitutions, pass the arguments as an array:
>> "[%s] And a number:%.1f" % ["Hey you dirty old ruby, put me in brackets. NOW!",43.4323]
=> "[Hey you dirty old ruby, put me in brackets. NOW!] And a number:43.4"

MySQL select query for grouping rows by frequency

SELECT DISTINCT field_name, COUNT(*) AS num FROM table_name GROUP BY field_name ORDER BY num DESC

Fancy little ruby input prompter

for i in 1..100
  print "Now at #{i}. Restart? "
  retry if gets =~ /^y/i
end

ruby is_numeric? extension to String

class String
  def is_numeric?
    Float self rescue false
  end
end

Crack open Firefox's new Places sqlite database with ActiveRecord

require "rubygems"
require "active_record"
require "active_support"

ActiveRecord::Base.establish_connection(
	:adapter => "sqlite3",
	:database => "places.sqlite"
)

class MozHistoryvisit < ActiveRecord::Base
	belongs_to :place, :class_name => "MozPlaces", :foreign_key => "place_id"
end

class MozPlaces < ActiveRecord::Base
end

p Time.now.yesterday
from = Time.now.yesterday.to_i * 1000000

MozHistoryvisit.find(:all, :conditions => ["visit_date > ?", from]).each do |h|
	place = h.place
	puts place.title
	puts place.url
	puts place.visit_count
	puts
end

« Newer Snippets
Older Snippets »
Showing 11-20 of 66 total