DZone 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

Snippets

  • submit to reddit

Recent Snippets

                    Returns a random key from your hash, but won't repeat until it's done every one.  (I was using this to help quiz myself.)
class Hash
  @keys_used = []

  def random_key
    @keys_used = [] if @keys_used.size == self.size
    key = self.keys[rand(self.size)]
    while @keys_used.include?(key)
      key = self.keys[rand(self.size)]
    end
    @keys_used << key
    return key
  end
end
                
                    
| w s p |

w := SystemWindow new.
s := ScrollPane new.
p := PasteUpMorph new.
p extent: 1000@1000.
s scroller addMorph: p.
w addMorph: s frame: (0@0 corner: 1@1).
w openInWorld.
                
                    | oldContent newContent |
oldContent _ nil.
[[Smalltalk at: #Stop ifAbsent:[true]] whileTrue: [
newContent _ 'http://minnow.cc.gatech.edu/squeak/recent' asUrl
retrieveContents contents.
(oldContent ~= newContent) ifTrue:[
(FMSound majorChordOn: FMSound flute1 from: #c4) play.
oldContent _ newContent
].
(Delay forSeconds:  30) wait.
]] forkAt: Processor userBackgroundPriority                
                    
(ProtoObject withAllSubclasses asArray 
collect: [:each | each subclasses size]) asBag sortedElements
                
                    | myPen |
myPen := Pen new defaultNib: 2.
myPen up.
myPen goto: Sensor mousePoint.
myPen down.
[Sensor waitButton. Sensor yellowButtonPressed] whileFalse: 
    [Sensor redButtonPressed 
        ifTrue: [myPen goto: Sensor mousePoint]].                
                    GIFReadWriter putForm: Display onFileNamed: 'screenshot.GIF'                
                    dir := FileDirectory on: 'd:\\images'.
dir keysDo: [:name | form = Form fromBinaryStream: (dir fileNamed: name).
ImageReadWriter putForm: (form scaledToSize: 512@512) onFileNamed: 'scaled_',name;
putForm: (form scaledToSize: 80@80)onFileNamed: 'thumb_',name]                 
                    'http://some.server/dir/file' asUrl retrieveContents contentStream fileIn                
                    from http://simon.incutio.com/archive/2004/05/26/addLoadEvent

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
To add an event:
addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  /* more code to run on page load */ 
});
Tested on IE 5, 5.5 and 6 for Windows; IE 5 and Safari for Mac; Opera 7.5 and FireFox on Mac. Fails on Opera 6.                
                    This script lets you update your IP address at everydns.net

#!/usr/bin/env ruby
require 'optparse' 
require 'net/http' 
require 'cgi' 

class EveryDnsUpdater
  attr_accessor :user, :pass
  attr_reader :ip, :code, :body

  def params
    @params ||= {"ver" => "0.1"}
  end
      
  def host
    "dyn.everydns.net"
  end
  
  def query
    "/index.php?" + params.collect{ |k,v| "#{k}=#{CGI.escape(v)}"}.join('&')
  end
  
  def parse(body)
    @body   = body
    @code   = @body.scan(/Exit Code: (\d+)/i).to_s.to_i
    @ip     = @body.scan(/IP now: ([\d\.]+)/).to_s
  end
      
  def run
    Net::HTTP.start(host) do |http| 

      req = Net::HTTP::Get.new(query) 
      req.basic_auth(user, pass)
      response = http.request(req)
      case response
      when Net::HTTPSuccess
        parse(response.body)
        return @code
      else
        return response.code
      end
    end
  end
end

opts = OptionParser.new 
updater = EveryDnsUpdater.new

opts.banner = "Usage: everydns [options]"
opts.separator ""
opts.on("-u", "--user=USER", "User name") { |str| updater.user = str } 
opts.on("-p", "--pass=PASS", "Password") { |str| updater.pass = str } 
opts.on("-d", "--domain=DOMAIN", "Domain name you wish to update")  { |str| updater.params["domain"] = str } 
opts.on("-a", "--address=[IP]", "IP address you want to supply") { |str| updater.params["ip"] = str } 
opts.on_tail("-h", "--help")   { puts opts; exit } 

opts.parse(ARGV)

if ARGV.empty? or updater.user.nil? or updater.pass.nil? or updater.params["domain"].nil?
  puts opts
  exit
end

if updater.run == 0
  puts "IP address set to #{updater.ip} successfully..."
else
  puts "Not successful..."
  puts updater.body
end