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

                    To create a borderless JButton in swing use the following:

JButton taskButton = new JButton(action);
taskButton.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
taskButton.setHorizontalAlignment(JButton.LEADING); // optional
taskButton.setBorderPainted(false);
taskButton.setContentAreaFilled(false);

This is useful for "hyperlink" style buttons or "task buttons".
                
                    To change all .htm files in a folder to .html files:

for f in *.htm; do mv $f `basename $f .htm`.html; done;
                
                    Go back to your previous directory:

cd -

Go to a new directory and push the current one onto a stack:

pushd /newdirectory

Pop a directory from the stack and go to it:

popd
                
                    
things = [] #[1,2,3]

if things.each do |thing|
    puts thing
end.empty? then
    puts "no things!"
end
                
                    Example of a Auto Attendant menu.

[MainMenu]
exten => s,1,SetVar(NUMINVALID=0)
exten => s,2,Setvar(NUMTIMEOUTS=0)
exten => s,3,Background(sales_press_one)
exten => s,4,Background(support_press_two))
exten => s,5,ResponseTimeout(5)
exten => s,6,DigitTimeout(2)

exten => t,1,SetVar(NUMTIMEOUTS=$[${NUMTIMEOUTS} + 1])
exten => t,2,Gotoif($[${NUMTRIES} < 2]?s,3)
exten => t,3,Background(vm-goodbye)
exten => t,4,Hangup()

exten => i,1,SetVar(NUMINVALID=$[${NUMINVALID} + 1])
exten => i,2,Gotoif($[${NUMINVALID} < 4]?:10)
exten => i,3,Background(invalid)
exten => i,4,Goto(s,2)
exten => i,10,Playback(vm-goodbye)
exten => i,11,Hangup()
                
                    
sudo port install ruby
sudo port install rb-rubygems
sudo gem install rails --include-dependencies
sudo ln /opt/local/bin/ruby /usr/local/bin/ruby

Originally from http://labs.broqn.com/blog/2005/03/ruby-on-rails-with-darwinports/ (added --include-dependencies)                
                    
cvs -d :pserver:anonymous@anoncvs.opendarwin.org:/Volumes/src/cvs/od login
cvs -d :pserver:anonymous@anoncvs.opendarwin.org:/Volumes/src/cvs/od update -P darwinports

or, if you didn't get the source from CVS:

sudo port -d selfupdate
                
                    if you do not already have a key pair
$ ssh-keygen -t dsa

send your public key to each host you ssh into
$ cat ~/.ssh/id_dsa.pub | ssh you@host 'cat - >> ~/.ssh/authorized_keys2'

now you can use ssh-agent and ssh-add to facilitate a "single signon" situation.

useful if you're ssh'ing into a bunch of machines all day.

google ssh-agent for distribution-specific details.                
                    
File.dirname "http://foo.com/bar/stuff.html"
#=> "http://foo.com/bar"

File.basename "http://foo.com/bar/stuff.html"
#=> "stuff.html"

File.split "http://foo.com/bar/stuff.html"
#=> ["http://foo.com/bar", "stuff.html"]

File.join( File.dirname("http://foo.com/bar/doc.html"), "relative_link.html" )
# => "http://foo.com/bar/relative_link.html"

(Originally by Ilmari Heikkinen on the ruby-talk mailing list.)                
                    In your controller:

  layout :my_layout_func

  ...

protected

  def my_layout_func
   # anything ruby will work
   ['list','index',nil].include?(action_name)? 
   'noheading':'my_layout'
 
   # or you could do
   case action_name
    when 'monkey': 'monkey_layout'
    when 'edit'  : 'form_layout'
   end
 end