Bringing the Technofunk

No one needs to read this site. Publishing this content is meant to force me to research new things, thus helping me to grow as a developer. If you think the things I post about are cool, then well... cool. If not, no big deal.

Tuesday, October 23, 2012

While I've used VIM in the past to modify text files in *nix, it's always been a forced affair, with me using it as little as possible to get things done. I've decided to dive into VIM, and try it out as a replacement for my standard code editor, TextMate. After all, I don't need much, just easy text editing, and syntax highlighting. So far, I dig it. Of course it helps that you can install VIM on practically anything (if isn't already there).

Monday, January 30, 2012

Per Wikipedia, a closure is defined as: "In computer science, a closure (also lexical closure, function closure, function value or functional value) is afunction together with a referencing environment for the non-local variables of that function.[1] A closure allows a function to access variables outside its typical scope."

Taken on just its pure definition, it's a little difficult to conceptualize, especially for someone like me who comes from a mostly iterative programming background. So let's look at some sample code (I modified some sample code about Ruby from Wikipedia)

Tuesday, September 13, 2011

The default validation handling of the activerecord validators is pretty nifty, in that it automatically does so much for you, like giving the view messages to display to the user, automatically saving user input in the form, etc. Here's how to manually throw an error that functions just like the validator errors.

In your model:
before_validation do
if something
self.errors.add(:some_field, "already exists")
end
end

If you have the appropriate setup in your rails form to display the error messages:

Then when you do an @model.save with invalid data, your view will automatically display the following to the user:
"some_field already exists"

Monday, August 22, 2011

I frequently find myself having to open the same set of applications in order to work (textmate, terminal with several tabs, browser, etc.). So I figured I'd spend some time figuring out how to automate the process.

open automator

create a new application

find "run shell script" <-- try it with this one first, if it doesn't work, use "run applescript"

input the following into the shell scripting area (it's a rather small textarea). Change the settings for your particular set of applications you need to use. For me, I want to open my default browser to my local app, open textmate with my project, open my text file with my ongoing notes, and open several terminal tabs with various commands already entered.

open http://myapp.localhost
open ~/Work/myapp/myapp.tmproj
open ~/Desktop/myapp.rtf

osascript 2>/dev/null << EOF

tell application "Terminal"
activate
end tell
tell application "system events" to tell process "Terminal" to keystroke "t" using command down
tell application "system events" to tell process "Terminal" to keystroke "t" using command down
tell application "system events" to tell process "Terminal" to keystroke "t" using command down
tell application "Terminal"
do script with command "cd ~/Work/myapp; rails console" in first tab of first window
do script with command "cd ~/Work/myapp/log; tail -f development.log" in second tab of first window
do script with command "cd ~/Work/myapp; su -" in last tab of first window
end tell
EOF

save it and put it in your applications directory (it doesn't matter where you put it, but I figure this is the best place)

Saturday, July 9, 2011

I've been playing around a bit with PostgreSQL on OSX, and I had to use a bit of *nix/bash general knowledge to get it working, hopefully this can save someone else a little time.

After running the installer that you can find here: http://www.postgresql.org/download/macosx

Switch to root ( su - ).

Then su - postgresql (the installer creates a postgresql user on your box, but you can't login directly as that user, except through root since su - as root bypasses password checks).

Go to your bin directory for PostgreSQL (default on OSX through the install wizard is /Library/PostgreSQL/9.0/bin) and run ./createdb -s -P yourusername (I used my default OSX user). It will prompt you for a password that you want to use for this user. I used my default OSX user's password. One less thing to remember.

Exit back to your default user.

In your .bash_profile, add this to your PATH variable :/Library/PostgreSQL/9.0/bin (change this if you installed to some other directory).

Be sure to run a source ~/.bash_profile so this variable takes effect in your current terminal session.

Tuesday, April 12, 2011

This one stumped me for a bit today. If you had any old code from Rails 2.3.* and converted it to Rails 3.0.*, watch out for any instance variables in your controller that you call to_json on for processing by jQuery's $.parseJSON in your view.

Because the default in Rails 3 is to make all strings html safe, you no longer need to use the "h" helper method in your views. However, this means that code like this will no longer work:

controller: @temp = {:hello =>; "world"}

view: alert($.parseJSON("<%= @temp.to_json %>"))

You will get invalid JSON errors, because the "safe" string is not in a valid format for parseJSON. To solve this, use the Rails 3 html_safe helper to tell the view that this string is ok to send through as is: