Ruby has a neat feature called at_exit which takes a block and then executes the contents of this block when the program ends. There are a couple of VERY important details:

  1. It takes a block and converts it into a Proc object at the time of parsing. This means that the data has to be available in the binding, or you’ll run into errors. Example: instance variables need to be set before you can use them in that block. Better idea: don’t use instance variables in there at all.
  2. You can ‘chain’ at_exit calls, and they will be resolved in a First In, Last Out (FILO) order.

Once you know this, using at_exit and writing tests for it becomes a little easier:

class Piddler
   def initialize
     create_pid_file
   end

   private

   def create_pid_file
     pid_file = "/tmp/piddler/my_pid" #Simplified for example purposes
     File.new(pid_file, 'w')
     at_exit { FileUtils.rm_f pid_file }
   end
end

What you’ll notice is that the at_exit block is defined RIGHT AFTER I create what I will need to resolve/undo/finish – not separately, right inside the method.

 

def test_clears_pid_file_when_it_exits
   at_exit { assert_equal 0, Dir['/tmp/piddler/*].size}
   Piddler.new
end

The advantage of that is that I know exactly when it gets defined. For this example, it gets defined at the end of the ‘initialize > create_pid_file’ call. This means that any at_exit blocks defined BEFORE that will be resolved AFTER.

 

 

We’ve been using git at work for a greenfield project and, so far, this has worked for us. It could be a useful template to get another team started.
We have gotten rid of the name ‘master’, which is only a convention anyway, and instead are using a few permanent branches:

  • production (only stable code which gets deployed to production goes there. The only branch to be merged in, EVER, is ‘stable’)
  • stable (only stable code which goes to the staging environment goes there. The only branch to be merged in, EVER, is ‘integration’))
  • integration (code from other branches goes here – tests should, of course, pass before merging in.)

And then we have, of course, other branches for work in progress and other stuff. we’ve used these prefixes to help with the naming, and called this the ‘buffers’ convention (look at the first letters – BFRS):

  • bug_
  • feature_
  • refactor_
  • spike_
How have you organized your git repositories?
 

Table of contents for Getting started with a programming language

  1. Ruby

First, get Ruby. Install it as explained on the website. Once you’re more comfortable with it, in a few weeks, you can check out RVM. Not now though – you’d just confuse yourself.

The two important command-line tools are going to be “irb” and “ruby”. For development, you will be able to use tools like emacs, vim, Jetbrains Rubymine, or Eclipse. Ruby support is quite widespread.

Create a temporary directory and get cozy inside.

Right off the bat, how would you write a test?
You would create a file called “my_test.rb” (It is a good naming convention, ‘something_test.rb’).

require 'test/unit'
class MyTest < Test::Unit::TestCase
def test_addition_is_not_broken
assert_equal(10, 5+5)
end
  def test_addition_is_not_broken_but_i_am
    assert_equal(10, 5)
  end
end

A couple of things of note:

  • “require” is how you load external libraries.
  • The test class name (“SomethingTest”) is a convention. Follow it and avoid pain.
  • The test method name also follows a convention : “test_” . Again, follow it and avoid pain.
  • Indentation is two spaces by convention. Follow, Avoid.
  • Parentheses for method calls are optional, but it’s good form to put parentheses if there are two or more arguments to a method. Also, parentheses will save you if the parser has issues parsing. Remember that.

To run that, you would simply call “ruby my_test.rb” and see one test succeed and one test fail.

How would you write a simple program?
Create a file called “myprog.rb” and put the following inside:

def palindromize input
  input.reverse
end

puts "Give me something and we'll see if it's a palindrome!"
input = gets.chomp
puts palindromize(input)

This would be run, like the test, with “ruby myprog.rb”
Some things to note:

  • “gets.chomp” — Ruby, by default, captures the return key being pressed. Chomp is necessary to remove that special character.
  • Did you notice how I put parentheses around “palindromize(input)” ? It’s so that both the Ruby parser and human readers stay sane when reading the code.

Things of note:

Ruby documentation: http://ruby-doc.org/, http://ruby-doc.org/core — that will get you started. The documentation is excellent until you get to some arcane features — so not to worry, for now.

Wanna talk to people? You can go to http://www.ruby-forum.com/ (Ruby big names, such as David Black, JEG2 and even Matz, the creator of the language) are active there, and everyone is nice. You can also go to the Freenode server on IRC, channel #ruby.

Let me know if you think I could add things to this tutorial or if you have questions!

 

Hello, and welcome to a blog series about programming languages. The goal is to explain how to get started in a few quick steps : install, write a simple test, write a simple program. Hopefully, just enough information to get you started without getting in your way.

I recommend you try your chops with http://projecteuler.net – fun algorithm implementation. A good way to learn a programming language.

When talking to people, please remember How to Ask Questions the Smart Way.

 

On Monday, April 26th, I am starting a new job at Cyrus Innovation. It may well be fair that I am starting a new career – it’s a hop, skip and jump for me. I’m switching from Systems Administration / Tech Support / Hell desk (not a typo, sadly) to Development / Programming.

I am very much looking forward to it. It means a hobby/job switch: I will now do sysadmin stuff as a hobby, and programming as a full-time job. We’ll see how it works out :)

I remember looking up to the early programmers in awe – they were wizards, and their skill probably couldn’t be equaled, reached, or even, likely, attempted. Then came Ruby. There will always be master hackers – but with Ruby, and its community, it seems that everything is easily at hand. People care about making code elegant and accessible. Things which would take lines and lines of codes in other languages are usually between one and four lines. It’s powerful. And it’s not difficult. The most arcane and powerful elements of the language are … Accessible. The code doesn’t get in the way of the mental gymnastics.

I’m sure I’ll be using other languages – it’s a good way to grow, after all – and I look forward to it. Finally: a place where I can, and will, always be learning.

 

One can’t actually move a remote branch, but you can copy a branch and delete a branch, so…

Copy oldbranch in repo to newbranch.

git push {repo} {oldbranch}:heads/{newbranch}

Ex: git push origin foobranch:barbranch
renames foobranch to barbranch

Remove a remote branch: it’s all about the colon:

git push <remote_repo> :heads/<branch>

Example: git push origin :heads/some-branch removes some-branch from the remote repo (apparently git push origin :some-branch works as well).

This works for removing a tag as well: git push origin :sometag

 

So, I play weiqi (known as the game of go, but I prefer using the chinese name).
There are some SGF parsers out there for Ruby, but I wanted to write my own – the other ones didn’t seem comprehensive enough, didn’t seem Ruby-like enough in their objectifiying the women data.
My original goal was twofold: write a script to rename all the SGF files in my collection so they can be easily parsed later, and … Learn more Ruby!
Here is the github link: http://github.com/Trevoke/SGFParser
It’s a work in progress. Right now it parses the SGF file properly, but the user has a fair amount of work to do to use the data.. Which, maybe, is the way it needs to be. But I’m gonna try to follow the principle of least surprise to the bitter end! :)

 

This is known to work on ASE 12.5.1 – you’re welcome to let me know what else worked, I am particularly curious about later versions of Sybase ASE, such as 15.

Prerequisites:

  1. Sybase PC Client 12.52
  2. The Ruby Sybase  files
  3. gem install activerecord-sybase-adapter -s http://gems.rubyonrails.org

Put the Sybase files in C:\ruby\lib\ruby\site_ruby\1.8\i386-msvcrt .

Copy all the DLLs in %SYBASE%\OCS-12_5\dll and put them in %RUBY%\bin (if you are not familiar with Windows notation, this simply means to go where you installed each of Sybase and Ruby and then go to the proper subdirectory). Why all of them? Well, because I’m too lazy to know which ones we -do- need.

Set some environment variables, just to make sure Sybase works fine:

Right-click on My Computer, Properties > Advanced > Environment Variables
If you have full permissions, go ahead and add those as SYSTEM VARIABLES.. Otherwise, well, only for your user (but aren’t you doing this on a server?).

SYBASE=c:\sybase    (where you installed Sybase).

JDBC_HOME=c:\sybase\jConnect-6_0   (again, where you installed Sybase, then the subdirectory).

CLASSPATH=%JDBC_HOME%\classes\jconn3.jar (just make sure that’s the right number).

If you’re going to create a user on your Sybase server for specific Rails usage, it needs to have SELECT permissions on the following : sysobjects, syscolumns, systypes, syscomments.

Edit your %SYBASE%\ini\sql.ini and add the information for your server in there.

Next.. In Rails, the magic will look like this in database.yml :

production:
adapter: sybase
database: <YOUR_DB_HERE>
username: <USERNAME>
password: <PASSWORD>
host: <NAME_GIVEN_IN_SQL_INI_HERE>

And that should be pretty much it!

Don’t forget to edit your model if you’re working with legacy data:

class Patient < ActiveRecord::Base
set_table_name "some_odd_name"
set_primary_key "some_primary_key_column"
end
 

I want to have a variable number of text fields in a form, and sending all of these text fields to one function when it is submitted. My best guess is that they’d have to be passed in as an array, unless there’s a more elegant solution (akin to ARGV in Ruby, or the arguments passed in to a program that starts.. ?).

Does anybody have an idea?

© 2011 Seven steps Suffusion theme by Sayontan Sinha