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



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

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


Thanks,
Branden


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

Sunday, October 24, 2010

i18n bug in Rails




I just ran into a bug when we installed a new gem in the same Ruby install that runs both our test environment and our Redmine install. The problem is that the new i18n gem is not compatible with rails 2.3.5.

The solution was to change this line
gem 'i18n', '>= 0.1.3'

to this
gem '18n', '<=0.4.1'

To ensure that Redmine grabs an earlier version of the i18n gem. Hopefully this saves someone else out there from a few hours of frantic googling.

Saturday, July 24, 2010

Repository browsing with redmine

Redmine plays well with Git. It was so easy to set up, everyone who uses redmine and git should do it.


Using Redmine is now my default way to browse repository commits.

Tuesday, July 13, 2010

f.lux is saving my eyes

Like many technology workers, I suffer from overuse injuries of the fingers and wrists, as well as constant eyestrain. I do much of my programming at night with the lights off, and while OSX has good built in functionality for dimming a monitor (or using the Shades app for secondary monitors), it didn't seem like enough.

I could either have the brightness turned up so I can see, but have my retinas blasted with excess photons, or have the brightness turned down, and then squint as I try to read.

Enter f.lux.

Instead of messing with the brightness of your monitor, f.lux changes the colors of your monitor to be easier on the eyes. At first, it was kind of an uneasy yellow color, but my eyes adapted quickly.

Everything felt normal, until I turned f.lux off. My monitors colors returned to normal, and I instantly felt like icepicks were being driven into my eyes. Needless to say, f.lux was turned back on, and I use it every night. I heart f.lux.

Thursday, May 13, 2010

Git

Git is so much easier to work with than subversion, it makes me wish we had switched sooner.

Tuesday, April 6, 2010

More on simple databases

When I think about it some more, the difference between something like MS SQL Server or DB2 vs Hibernate + MySQL with MyISAM, is that the traditional way is for the RDBMS to be a single system, relational management AND static data storage. The persistence layer/db strategy is separating the RMS (relational management system) from the DB (static storage). By separating out the relational management portion, and moving that into the persistence layer, you have the ability to run one processing cluster which serves your application and handles the persistence layer/rms, as opposed to running two clusters, one to serve your application, and one to handle the traditional RDBMS. I would think that combining your servers into one larger cluster to handle more of the computing would be more efficient than splitting it into two clusters, in which case one cluster may hit max load while the other sits idle.

By the way, I know that there are measurable differences between MyISAM, InnoDB, or other storage engines, like row level locking as opposed to table locking on updates.

How I'm learning to love simple databases

Once upon a time I was a total believer in strict database rules. Everything should be fully normalized, schema and diagrams thought out ahead of time, strict data validation at the database level, so on and so forth. A total believer in the primacy of MS SQL Server, DB2, so on and so forth. Within the last 6 months, I've had a change of mind that's been brewing, and only lately have I more fully realized it.

When you're developing a webapp nowadays, chances are you have various frameworks and technologies that you use, each one layered on top of the other. MySQL -> Hibernate -> Java application -> JSP/Servlets -> HTML/markup, as one example. The data that you accept in from your users must pass through each layer to finally get to your storage, whether it's in memory at the application layer, or static storage at the db. Each layer has its own data validation. So if you have a date field that a user fills out, it comes in as a string, which the Servlet can accept in and convert to a valid Java date, which passes it to your application layer, in which you can put that data in an object that checks that it's a valid date (if it's defined as a Date object). Then it goes to your persistence layer, which in our example, Hibernate would check that it's a valid date. It finally gets passed to your database layer, which in our example, MySQL would check that it's a valid date. All that work to verify that it's a date several times. Why? What's the point? If you checked that it's a valid date once, and you don't do any measurable transformation of the data, there is no need to check it again.

If I think about it, when is it exactly, that I care that I check if it's a valid date? When I accept data in from the user, when I do fancy things at the application layer like date/time arithmetic, and when I display data back to the user. None of that touches the database layer. Do I care if the static data is stored in a traditional RDBMS, a bunch of XML files, or a straight up B tree like GT.M? Other than the possible performance gains by storing static data one way or another, it doesn't matter.

I currently use Wicket, and Wicket has easy to use form validation built into the framework itself, which covers all the cases in which I would care to check if the data is appropriate or not. Right now, I do datatype management at each layer because that's how I was taught, but I'm seriously considering switching everything underneath Wicket to string, integer, or float, crossing my fingers, and hoping nothing blows up.

Beyond data validation, what about primary/foreign keys? Many databases don't even have pk/fk relationships built in. Originally, I thought those databases were mad (in a bad way). However, if you think about it, the business logic which says that one table must relate to another table via a field based relationship, that business logic has to exist in your RDBMS, your persistence layer, and your application (because you must pass data with the appropriate relationships to the layer below). Why should each layer check the relational integrity of the data? The only case I can think of, is to protect myself from making a mistake at the application layer. Fine then, I can set those rules in the persistence layer as well as my application, and the database can be simply a static container, nothing more.

Wednesday, July 22, 2009

I Heart Automator

Every time I wish there were some way I can automate something in OSX, I find out there is a way, through the nifty scripting utility Automator.

I had a particular problem I wanted to solve, which was to add spotlight comments to multiple items. This way I can do dynamic searching of files based on keywords instead of creating an entire folder hierarchy and having to drill down to get to a specified file.


And here's my Automator plugin in action, accessible right from the finder menu. Slick!



Friday, May 8, 2009

MySQL Auto Data Conversion

So I've been researching MySQL, and I have run into something that kinda boggles my mind. Whenever you give MySQL the wrong datatype for a field, MySQL attempts to do autoconversion of your data. This in itself isn't a bad thing, and some of them may make sense, such as doing string to int or int to string. However, if you pass MySQL data that it is out of bounds, MySQL chooses data that is in bounds, and places that data in the field instead. So if you have a field defined as a tinyint, whose bounds are -127 to 127, and you pass it 1,000, MySQL will input 127, and consider it a success. 

You have to write your own MySQL specific data validation in your application BEFORE you pass it to MySQL..... Say it with me..... WHAT??? The whole idea with abstraction and error catching is that each layer of your software stack should be able to pass "something" to another layer and receive back success or failure, at which point the originating layer can decide how to handle it. By having your application layer have to do database validation checks BEFORE passing data to MySQL, you loose the benefits of abstraction, and by definition, you loose the benefits of modern software engineering. 

Somebody out there explain to my how MySQL's handling of out of bounds data is a good idea, cause I don't get it.

Keynote 2008

So I've just done my first presentation with Keynote 2008, and color me impressed. When you compare iWork vs MS Office, I consider Excel better than Numbers, and Word better than Pages. Keynote however, is far better than Powerpoint. Both programs have a practically identical purpose and workflow, but where Keynote excels is in the quality of the default templates, animations, and graphics. Basically those things that taken separately, don't matter much, but taken as a whole, it makes Keynote the superior presentation building application.

I wonder if OSX 10.5.7 will be included in Mac Box Set Family Pack. If so, I may have to pick that up so that I can iWork 2009, iLife 2009, and OSX 10.5.7

Saturday, March 7, 2009

Automator for OSX

I've begun using a software VPN utility called Hamachi, owned by the guys at logmein (every family IT dude's friend). Alas, there is a slick Windows client, but the OSX one is pure command line. I'm totally fine with that, but I figure "let's see if I can make it all OSeXy". So I created a bash script to execute all the Hamachi commands, and I combined that with some Applescript and OSX's Automator, which is kind of a wysiwig script editor. I could have done it all in bash or Applescript, but I wanted to put my script on the dock and run it from there. Only Automator "executables" can be moved directly to the dock.

Here's a sample from my automator window. Note how cool it looks to have a wysiwig script editor!


So the end result is I have a clickable "thing" on my dock which will start up or quit the OSX Hamachi client. I also added in a Growl notification for fun, which is shown below.

Sunday, January 25, 2009

Evernote

Evernote is one of a long line of notetaking applications, from Microsoft OneNote (which is supposedly pretty good) to Zoho. In my search for a notetaking application to supplement my brain (which has horrible short term memory), I settled on Evernote as the most full featured of the bunch:
  • Has a desktop AND web AND iPhone client
  • For notebooks that are synced to Evernote's servers, free OCR
  • The ability to have synced notebooks (to take advantage of web/iPhone clients and free OCR), or desktop only notebooks (for sensitive info)
  • The ability to add pics, pdf, word, excel, numbers, pages, webpages, or free text to a note
  • quick searchability

The only thing remaining for me is to put my local Evernote notebooks into my truecrypt partition, then I'd be all set

Friday, December 12, 2008

Remember The Milk

I was a dedicated omnifocus user, but I just switched to Remember The Milk. If it was just my notebook or a desktop, omnifocus is the top GTD app hands down. However the omnifocus iPhone app is just tooo..... clunky. I have all the usual complaints about it, it loads slow, too much navigation to do simple things, so on and so forth. To me it seems like their developers are too used to the high horsepower of modern macs, and are having a hard time writing an app for a processor/ram/hd constrained product like the iPhone.


Remember The Milk on the other hand, is a pure webapp for macs, with a syncable iPhone app. The webapp is slightly slower than omnifocus for mac, which is understandable. However, their webapp has gmail like keyboard shortcuts, which is nice. I do miss omnifocus's ability to add a new task from anywhere. Remember The Milk also integrates well with gmail, I subscribed to my task list as a calendar in iCal, and has nifty settings like "postpone", and understands natural language as you input data. Unfortunately for omnifocus, the faster Remember The Milk iPhone app made me a switcher. 

Sunday, September 28, 2008

Stanza for the iPhone



I recently downloaded both the OSX and iPhone version of Stanza, with the intention of turning my iPhone into a good pdf reader. The verdict? Stanza on the iPhone is awesome for reading newspapers, books and magazines. Stanza is bad for reading tech documentation, especially those with diagrams and pics. I had hoped that Stanza might be good for tech docs, but alas my hopes were dashed. I still use it on an almost daily basis though, since there's a lot of classic books that are now in the public domain.