Tuesday, March 10, 2009

The path to happiness...

I suppose programming prodigies begin thinking in kindergarten about automation. When learning how to write letters they must think "the letter 'o' should really be its own class, and then making the letters p, g, b, and q inherit from o, since they are, in essence variations of the o-theme." But the rest of us went through leaning the letters one letter at a time. Without thinking about automation, or packaging actions in reusable forms. The Chinese and Japanese certainly understood modular writing. In the figure above is a set of Kanji related to rain--cloud, snow, frost, fog, hail etc. all inheriting from the symbol for rain. This type of thinking in reusable chunks of information is probably essential to human happiness. The wonderful thing about recognizing a reusable ideas, is that it takes much of the effort out of their application. So in celebration of the encapsulated, reusable concept and its gift of carefree automation (=happiness?), I thought I would show how to add scripts to ruby so that those ideas will always be available for use. I use the RUBYLIB ENV variable as follows.

1. open the /Users/yourname/.profile and add:
export RUBYLIB="/path_to_your/folder"

this is a UNIX file that allows you to set up the starting environment for not only UNIX but other programs, like ruby, which run inside UNIX. That line will add your folder path to the $: variable in ruby (which holds all the paths used for searching for modules).

2. in irb
require "your_file"
=>true

your file is now loaded

3. use the code in the file
Thing.aI(Thing)

my file defines a class, Thing, that can tell you about objects sent in as parameters. The method aI stands for 'all Information', which gives information on ancestors, methods, constants etc. It is the equivalent of pythons dir(), which I mentioned in a previous blog.

Tim:~/Documents/Scripts_Ruby/usable> irb
>> require "Thing"
=> true
>> Thing.aI(Thing)
___Class_ANCESTORS___
Class
Module
Object
Kernel
___Class_CONSTANTS___
___Class_METHODS___
c
m
anc
sM
aI
___Class_SINGLETON_METHODS___
anc
c
yaml_tag_subclasses?
aI
sM
m


No comments: