Wednesday, January 23, 2013

AutoHotKey script to disable windows key, capslock, etc for games

Here is a small AutoHotKey script that will disable alt+tab, left windows key, and caps lock while you are in a certain window. It maps them to Alt+F1x so you can map those in the game. F12 then becomes the Windows key for when you need to get out. http://www.autohotkey.com
#ifwinactive World of Warcraft
{
 LWin::!F12
 !Tab::!F11
 Capslock::!F10
 F12::LWin
} 

Friday, October 26, 2012

Rails 3.2 Double-Post on adding AJAX

If you're seeing a double-post after adding AJAX, and you have ever precompiled your assets, try deleting everything from public/assets.

Wednesday, October 24, 2012

Rails 3.2.8 AJAX super-basic example

This is an updated-for-rails 3.2.8 super-simple AJAX tutorial, for people who have already followed a basic rails tutorial. I found a lot of rails AJAX tutorials but just enough has changed since 3.2.8 I wanted to show an updated version.
First, my versions of things I am using!

carmen@ubuntu:~/ws$ uname -a
Linux ubuntu 3.2.0-31-generic #50-Ubuntu SMP Fri Sep 7 16:16:45 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
carmen@ubuntu:~/ws$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
carmen@ubuntu:~/ws$ rails -v
Rails 3.2.8

Now on to the good stuff!

rails new ponies
cd ponies
rails generate scaffold Pony name:string profession:string
rake db:migrate
rails s

Make some ponies...

http://localhost:3000/ponies/new

Update your destroy line in index.html.erb. Add :remote => true, :class => 'delete_pony' to app/views/ponies/index.html.erb. The line should now look like:

    <%= link_to 'Destroy', pony, method: :delete, data: { confirm: 'Are you sure?' }, :remote => true, :class => 'delete_pony' %> 

Create a file, destroy.js.erb, put it next to your other .erb files (under app/views/ponies). It should look like this:

$('.delete_pony').bind('ajax:success', function() {  
        $(this).closest('tr').fadeOut();
});  

Add format.js { render :layout => false } to your controller (app/controllers/ponies_controller.rb). Your destroy method should now look like this:

  # DELETE /ponies/1
  # DELETE /ponies/1.json
  def destroy
    @pony = Pony.find(params[:id])
    @pony.destroy

    respond_to do |format|
      format.html { redirect_to ponies_url }
      format.json { head :no_content }
      format.js   { render :layout => false }
    end
  end

Restart your server, and click 'Delete' on some ponies (Preferably Rainbow Dash), and viola, they just fade out! In Rails 3.2.8.

Saturday, August 8, 2009

Ruby and Postgres

I've been trying to learn Ruby recently (rails comes later), and as an exercise I decided to write an ETL connector to load transaction data into my financial database. getting it to work with Postgres was a few steps so I thought I'd post it here as it's a pretty self-contained example.

First do these steps
  • install postgres-server-dev
  • sudo gem install postgres
  • sudo gem install pg
  • gem contents pg will allow you to view examples



  • Here's my code that converts the csv file and loads it up. I could not figure out how to turn of autocommit (we all know how I feel about autocommit) so I just start a transaction asap. I also couldn't figure out how to see how many rows are inserted with each insert statement like in perl, so I just used the PK. Enjoy!



    require 'rubygems'
    require 'pg'
    raise 'usage: ruby usbank.rb csvfile' if not ARGV[0]

    @conn = PGconn.new({:host=>'localhost',:user=>'DBUSER',:dbname=>'DBNAME'})
    @conn.setnonblocking(true)
    @conn.exec('BEGIN')

    def getid
    @conn.exec('select max(txn_id::numeric)+1 as txn_id from txn')[0]['txn_id']
    end

    file = File.new(ARGV[0], "r")
    file.gets

    begin_id = getid()

    while (line = file.gets)
    @array = line.split(",")
    date = @array[0]
    item = "#{@array[1]} #{@array[2]} #{@array[3]}"
    amount = @array[4]
    amount.chomp!
    puts "#{amount} #{date} #{item}"

    query = "insert into txn (date, amount, cat_id, item, source_id, txn_id)
    select
    \'#{date}\',
    #{amount},
    0,
    \'#{item}\',
    5,
    ( select nextval('txn_id_seq'))
    where not exists (
    select 1 from txn where date= \'#{date}\' and amount=#{amount}
    );\n
    "
    @conn.exec(query)
    end

    end_id = getid()
    puts "#{Integer(end_id) - Integer(begin_id)} updated."

    @conn.exec('COMMIT')
    @conn.finish
    file.close

    turn off autocommit for psql

    Every time I fire up a new instance of postgres, I forget how to turn off autocommit for psql. Searching on it, mostly I found a series of people saying "just begin a transaction." This makes NO sense! All it takes is one time of forgetting to begin a transaction, then one forgotten or ill-formed where clause on your delete statement and you're sitting on the floor crying. I am not sure why this is the default. So here is a post that will hopefully bubble above the People Not Answering The Question.

    Anyways, it's pretty easy. Just

    cd ~
    vim .psqlrc
    and make it look like this
    SET AUTOCOMMIT OFF
    viola! Now you may enjoy the wonders of Postgres (including transactional ddl, my favorite), without having to keep looking over your shoulder.

    Friday, December 26, 2008

    How to Test the Coil on a 2003 Ford Focus

    I suspected my coil was bad on my 2003 Ford Focus and decided to figure out exactly how to test it. Unfortunately it was not completely clear so I had to aggregate multiple sources. Here is a (hopefully) coherent version:

    1. Take it out
    2. Measure the primary and secondary resistances according to this picture. If something is infinite, the coil may be broken. Be careful though, make sure you are testing it right and getting a solid connection.

    My car just needed new plugs & wires. I wanted to be sure that was the problem before I just replaced them willy-nilly though!









    Check out this thread for more info: http://www.focusfanatics.com/forum/showthread.php?t=180606