Tuesday, October 23, 2012

VIM, I dig it.

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).

It seems like my MUD skills are finally useful.

Monday, January 30, 2012

Learning about closures in ruby

Per Wikipedia, a closure is defined as: "In computer science, a closure (also lexical closurefunction closurefunction 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)

puts "learning about closures"
def create_closure(initial_value = 0)
  closure_value = initial_value
  return Proc.new do |x|
    closure_value = x
  end, Proc.new do
    closure_value += 1
  end, Proc.new do
one, two, three = create_closure
myval = 21
puts "one.call(myval) == #{one.call(myval)}"
puts "two.call == #{two.call}"
puts "two.call == #{two.call}"
puts "three.call == #{three.call}"

This outputs:

learning about closures
one.call(myval) == 21
two.call == 22
two.call == 23
three.call == 23

Each proc here is a closure, because the scope of each proc is bound to the scope of where it was defined. In this case, all of the procs are bound to the scope of the create_closure function.

So you can see that even though the execution of create_closure is finished, each proc still has access to the same local closure_value variable that was defined within create_closure function.

Tuesday, September 13, 2011

How to display an error message to the user, just like ActiveRecord's validators

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")

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

<% if @model.errors.any? %>
  <% @model.errors.full_messages.each do |msg| %>
    <%= msg %>
  <% end %>
<% end %>

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

To simply throw something that halts execution, do a:
raise "something here"

Then you'd get a full stop error message and stack trace.

Monday, August 22, 2011

Setting up your work environment with automator

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"
    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

  • 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)
  • then drag it to your dock

Saturday, July 9, 2011

Installing PostgreSQL on OSX

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
  1. Switch to root ( su - ).
  2. 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).
  3. 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.
  4. Exit back to your default user.
  5. In your .bash_profile, add this to your PATH variable :/Library/PostgreSQL/9.0/bin (change this if you installed to some other directory).
  6. Be sure to run a source ~/.bash_profile so this variable takes effect in your current terminal session.
Now your default OSX user can run PostgreSQL commands.

Tuesday, May 17, 2011

OpenSuSE and mod_proxy_html

Dear OpenSuSE,

What is the point of having mod_proxy_http but not mod_proxy_html installed by default for httpd? This means that out of the box, httpd on OpenSuSE can't run a reverse proxy. Why did you do this?


Tuesday, April 12, 2011

$.parseJSON with Rails 3's .to_json

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:

controller: @temp = {:hello =>; "world"}
view: alert($.parseJSON("<%= @temp.to_json.html_safe %>"))