Jun 20, 2012

Setting up zombie.js in Windows 7

I took about 2 hours to find the answer, even the web do not have the complete answer for it. So, it's better for me to post the steps involved.

Anyway, the key to the issue is to setup the PYTHON path to include the python.exe filename in it.

Here's the list of steps for your reference.
  1. Install 32-bit python 2.7, not the version 3.x
  2. Set environment variables:
    • NODE_PATH to {path to nodejs}\node_modules
    • PYTHON to {path to python 2.x}\python.exe
  3. Run on VS2010 command prompt (run as administrator)
  4. npm install plugin -g
Hope the above works for you.

Oct 10, 2009

Some useful rake commands to remember

Just a short note on rake commands. It is used in the command line and can call objects in the rails application. This will be very handy especially if you need to create some functions in your app and call it from the command line.

To start with, some rake commands that you'll remember after starting your first app.

I can see how many Lines Of Code (LOC) for my application, just by running:
rake stats

A migration file is an atomic change and undo change similar to changes made in database structure.

We'll need this everytime we create new migration file.
rake db:migrate
rake db:migrate_plugins RAILS_ENV=production

Better still, use the -T option to get rake to describe a list of possible actions
rake -T

And for more specific commands on db

rake -T db

Oct 9, 2009

SVN to Git

Apart from Ruby and Rails framework, mastering the SCM skill is a part of the checklist to be in the elite group. SCM is software configuration management and it is for managing changes in software.

You do not want to made some mistakes and realize all your hard work is lost, especially on the source codes.

I've first discovered SCM using SVN, of which a much improved version of CVS. This solves much of a problem when I need to work together to develop our software with other programmers.

Before using SVN, I need to zip up the source codes, copy it to my colleagues and then they do the changes and then we pass around. But tracking the changes is a cumbersome and error-prone task.

Since SVN, we have less problem with that, however, there is a problem too. SVN needs a central server, of which we need to synchronize (update, commit) with.

But lately, since I'm doing more open source and rails community seems to love Git, I've come to know this little tool that is once mastered, can't live without it. Git is different because it is a distributed SCM, unlike SVN which is a centralized SCM.

Sep 25, 2009

More Ruby Shortcuts

Class Getter and Setter

class Vehicle
  def model

  def model=(model)
    @model = model

or shortcut would be (equivalent to above):

class Vehicle
  attr_accessor :model

Possible attributes are attr_reader (getter only), attr_writer (setter only) and attr_accessor (both get/set)


We can use ? symbol as part of the name of the method such as

def purchased?

normally we do this way:

if purchased? then puts "Thank you" end
if not purchased? then puts "Want to buy?" end 
#can also use !purchased? to negate it

we can also make it more compact and readable:

puts "Thank you" if purchased?
puts "Want to buy?" unless purchased?

Sep 24, 2009

9 Things I Like About Ruby, So Far

#1. Parallel assignment
short and sweet

a, b, c = 1, 2, 3
a = b = c = 5

#2. Easy OOP
From free codes, modules, classes
Everything is an object

Standard methods are great, you can even add on methods too:
class String
  def my_method
    puts "Awesome!"

Then you can have "text".my_method

#3. Readable
The language is compact and readable

(1..5).each do |n|
  puts n
# hash is a comment

You can use { } or do end as blocks too

One line does it all:

a ||= 10

Instead of:

if !defined? a then
  a = 10

You got any more neat tricks to share?

#4. Ruby have Symbols
Starts with colon :tryme

Makes things much readable, it is not a string, but acts like a string.
Weird. But simple and elegant.

Imagine that it is a variable with string value of its own name.

#5. Hashes are Easy
These are just cool pair values (I created my own pairs class back in VB6)

Define it this way:

p = {} #initialize as array first
p['lousy'] = 1
p['moderate'] = 2
p['good'] = 3
p['great'] = 4

or this way, all in one line

p = {'lousy' => 1, 'moderate' => 2, 'good' => 3, 'great' => 4}

even tastier when you use :symbols with hashes

p = { :lousy => 1, :moderate => 2, :good => 3, :great => 4 }

#6. Flexible Variables
No more type mismatch!

In VB,
Dim f as Integer
f = "Try Me" 'Type mismatch error happens

You can assign different data types to any variables any time.
f = 1.5
f = "Try Me" #no sweat

#7. Ruby have great web frameworks
Rails, Merb, Sinatra, Ramaze and more

#8. Working with Strings, creating harmony to your code
#{var} is an easy way for value replacements within strings

We can also have methods like #{var.method} assuming it returns a string

Multi line strings are easy to work with
letter = <<WHATEVER
  Dear #{name},
  This is the content of the email...

#9. It's Open Source
Need to say more?

#10. Not in the list
Oh! And by the way, you can test some codes out with irb (Mac / Linux) or fxri (in Windows)

Learn Ruby in 20 minutes -> this is where I started.

And to avoid confusion, try to learn up and familiarize with Ruby codes before looking at Rails. I got confused and I thought this tip may help you. :)

Sep 23, 2009

Rails from a VB/SQL Perspective

If you're a database programmer like me and my colleagues, you may need to think a little different now.

Instead of tables, queries and triggers - forget about those. Now we are able to develop database independent software (provided you don't mess up with hardcoded SQLs).

To start with, first we have the Schema (which are the table structures). In Rails, the database design is encapsulated in the Model and Migrations. This are the 2 things you need to focus on.

Our logics on database can be taken care by ActiveRecord. Association (such as 1-to-many, many-to-many relationships) and data validations (such as required fields, FK fields with referential integrity) can be easily defined in the Models.

And best part, only 1 or 2 lines of codes will take care of the rest. This is a high-level DB programming compared to coding individual lines to read from different tables and validate on the data entered.

For example:

In User class
has_many :links
has_and_belongs_to_many :groups (this is my favorite, many-to-many)
validates_presence_of :name
validates_uniqueness_of :login, :email
validates_length_of :login

In Group class
has_and_belongs_to_many :users

My first confusion starts with the schema. How do I start? Model first or database first. Now, that I've cleared the confusion, we can forget about database. You can use scaffolding or generate a model instead by calling:

script/generate scaffold ModelName name:string notes:text
script/generate model ModelName name:string notes:text

The name:string notes:text is an example. You can ignore that as it is optional. Once done, you can go to db/migrate folder to edit the migration file created automatically for you.

Once you are happy with your migration files, you can then apply the changes to database by running rake db:migrate

You can repeat this for all the Models (or tables) you want to create.

It will also auto-generate a file called db/schema.rb, which will collectively add all the incremental changes / additions of your models.

For creating new database with the table structure, you can either apply the migrations with the rake db:migrate command or by using the schema file, which is rake db:schema:load that will speed up the process.

What are Callbacks?

In VB, it is called an event. Windows development are mainly event-driven programming. And callbacks are similar. In ActiveRecord, we can define callbacks :before_add, :before_remove, :after_add and :after_remove.

There's plenty more and you can refer to the AR documentation in http://api.rubyonrails.org or directly here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Sep 21, 2009

Rails advantages and techniques

Here is a nice summary of what Rails can do. Need more reasons to go into Ruby / Rails?