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 31-40 of 66 total

Ruby remote file checker

   1  
   2  require 'rubygems'
   3  require 'open-uri'
   4  require 'net/http'
   5  
   6  def remote_file_exists?(url)
   7    url = URI.parse(url)
   8    Net::HTTP.start(url.host, url.port) do |http|
   9      return http.head(url.request_uri).code == "200"
  10    end
  11  end

Mechanize / Hpricot / Scraping setup

   1  
   2  require 'rubygems'
   3  require 'cgi'
   4  require 'open-uri'
   5  require 'hpricot'
   6  require 'mechanize'
   7  
   8  agent = WWW::Mechanize.new
   9  doc = Hpricot(agent.get(the_url).parser.to_s)

Rails Regex: Convert spaces to dashes

   1  
   2  s.gsub!(/\ +/, '-')

Count the number of files in a directory

   1  
   2  ls -a | wc -l
   3  
   4  You can also try ls -A (same as --almost-all) to list all dotfiles but . and ..


Snagged from http://www.webservertalk.com/archive109-2004-7-315190.html

Move files using find and exec

   1  
   2  find . -name "xyz*" -exec bash -c "mv {} ./xyz-dir/" \;

Ignore a folder in subversion

   1  
   2  # In this case, ignore contents of 'coverage' folder in current directory.
   3  svn propset svn:ignore 'coverage' .

Rails Regex Lookaround

(Snagged from http://www.pivotalblabs.com/articles/2007/09/11/string-split-and-regex-lookarounds)

Ruby supports regular expression quite well, so it's not surprising that one can use a lookaround to match a specific position. That also works really well with String#split, so if one needs to extract the key/value out of a string such as this:

   1  
   2  s = "TYPE=Exterior RED=119 GREEN=105 BLUE=88 TABLEREQ=PNTTBL-01 TABLE=Primary w/XL GENERICCLR=Beige GENERICCLRCODE=31"
   3  s.split(/\s(?=[A-Z]*=)/)


Just splitting on "space" won't work because there are spaces inside the values, too ("Primary w/XML"). Simple and useful...

Backup all your del.icio.us bookmarks to XML

   1  
   2  https://api.del.icio.us/v1/posts/all
   3  curl --user accountname:password -o myDelicious.xml -O 'https://api.del.icio.us/v1/posts/all'

Remove all .svn folders from directory recursively

   1  
   2  find . -name .svn -print0 | xargs -0 rm -rf
« Newer Snippets
Older Snippets »
Showing 31-40 of 66 total