Tag Archives: ruby

Ruby Line Formatting

I wrote a little utility to turn something like this

this=that,
something_else="something else entirely"

Into the much easier to read

this           = that,
something_else = "something else entirely"

I use it in conjunction with my clipboard utility so I can just copy, run, paste. I mostly use this when I’m working with ColdFusion, so it also works for those pesky cfsets.

Usage: (After copying your target text to your clipboard)

ruby clipboard_format_set.rb

Download It!

PS: I’d love to set some of my scripts up as Eclipse shortcuts so I’ve been toying with the idea of writing an Eclipse plug-in to act as a proxy. I realize you can set up and run “External Tools”, but I have yet to see a way to bind a key. I’d like the plug-in to take care of the “pasting” to save me that extra ctrl-v. Maybe this would make a good New Years Project?

Ruby Clipboard Directory List Utility

I wrote this little script to copy the filenames of a directory into the clipboard a while back and forgot to post about it. I’ve been particularly surprised about how often it’s come in handy for this or that.

 require 'clipboard'

c = Clipboard.new

list = ''

if ARGV.size == 0
	path =  "."
else
	path = ARGV[0]
end

Dir.foreach(path) {
	|file|
	if(file.length > 2)
		list << file << "n"
	end
}

c.set_data list

To use it, simply call run the script with the directory you want copied to your clipboard as an argument!

Download the code!

Target Practice Pt 2

I made a few small updates to Target Practice tonight. The biggest addition was sound although I made a few other little tweaks under the proverbial hood.

I had originally wanted to pack it up and release it as a single executable file but I was having some issues which I’ll post about once I figure out what the heck I’m doing.

Speaking of lazing, there are a few things I’d like to do differently in Target Practice. Most notably I’d like to abstract out out the media to some manager or another, and also my target manager class has grown rather bloated which I take to be a good indicator that it’s doing more than it should. I don’t care enough about this program to fix either of these issues, but hopefully I’ve learned from these mistakes!

Download it!

Target Practice

Half-Life is the game of choice at work, and sadly I’m terrible at it. I thought I’d try to write a little game using gosu to try and help me improve my accuracy. I just reached a good stopping point and I thought I’d share.

It’s really simple, the idea is to shoot little 20 x 20 “targets” (I’m artistically challenged) before they expire. Missing or letting 10 targets expire will end the game. Next version will include sound, improved visuals and maybe even a couple other fun features if your lucky.

The code is a bit over-engineered but I plan on incorporating parts into some “core extensions” that I’m working on that will hopefully make it a lot easier for me to bang stuff like this out. Finally, as I doubt anyone will ever download this, I didn’t bother to pack it up so you’ll have to have Ruby and gosu installed in order to enjoy it.

Download the code!

PS: I’ve been a Photoshop user for many years, but I used GIMP to make these images and I didn’t even hate it!

Ruby Line / Curve…Thing

Remember these?

During a brief period in elementary school drawing these things was quite the cool thing to do. I’ve been messing around with the GD2 module a bit lately and I thought it’d be fun and easy to write a little script to generate one.

It was.

The drawing algorithm

@nodes.times do |i|
    adjustment = i * @node_size
    pen.move_to adjustment, 0
    pen.line_to 0, @size - adjustment
end

Example Usage:

#takes in the output image size and the number of nodes
c = Curve.new 180, 30
c.draw
c.export "test.gif"

Download it!

*Note: If your output size doesn’t divide evenly by the number of nodes then the image will actually reduce the size of the output image.

Ruby Resize Utility

I wanted to demo an ad rotator the other day at work and I wanted to load up a bunch of images of various sizes to make it look legit. It would have taken forever to do it ad by ad, so I wrote a little ruby script to automatically resize all the images in a directory. Then I used my directory listing utility to copy the files into my clipboard. Pasted into the database from there!

(Requires GD2)

Usage:

 r = Resizer.new input_path, output_path, width, height
 r.crop_files

Download it!

JavaScript Tile Puzzle

I was tinkering a bit while watching the tube tonight and I thought it might be fun to make a simple JavaScript tile puzzle. Might look terrible in other browsers.

First step was to write a little ruby script (using the GD Graphics Library) to read in a file and cut it up into individual tiles. (Photoshop is for wussies)

if __file__ = $0
  #pass the image name, tile size through the command line
  t = Tile_Cropper.new ARGV[0], ARGV[1].to_i
  t.crop_tiles
end

Then I used a slightly modified method I read about in Ben Nadel’s blog to scramble the tiles.


	
	
	
	
		
	
	

A little bit of CSS magic:

#puzzle {
	width:      500px;
	margin:     0px;
	padding:    0px;
}
.piece {
	list-style: none;
	display:    inline;
	margin:     0px;
	padding:    0px;
}
img {
	border:     1px solid gray;
}

And finally I used scriptaculous and prototype for the obnoxious drag-and-drop sorting. Not that I’m complaining. the Sortable methods weren’t intended to be used for something like this.

function init() {
	Sortable.create('puzzle');
}

Complete the puzzle to find out what I was watching!. Here, I’ll give you a hint. It’s Dexter.

Download the code.

Stack Overflow and Ruby Regex

I’ve been following the Stack Overflow podcast for a little while and I’ve been itching to come up with a question to ask on the site. Well, today I was working on a little ruby formating script (more on this later) and I wanted to replace all the spaces in a string that were NOT located between quotes.

I asked my question on the site and a few hours later I had some really great working solutions!

Click the link for solutions and definitely check out http://stackoverflow.com!