Thursday, August 23, 2012

Get user input using nodejs

I found a way how to get user input using nodejs (to differentiate from client-side js, I will refer to javascript server-side as nodejs)--
$ node test.js
prompt: username:  jim
prompt: email:
Command-line input received:
  username: jim

Here's the script for test.js--
var prompt = require('prompt')

prompt.get(['username', 'email'], function (err, result) {
  console.log('Command-line input received:')
  console.log('  username: ' + result.username)
  console.log('  email: ' +

I read from here that using semi-colons are optional. Notice that I wrote the script above to test if I'd get an error. I ran this script on nodejs CLI, and I did not get any error.

I am thinking that I can write statements, as long as I do one statement per line. I'll test this in subsequent nodejs scripts from here on. (*EDIT... I am not totally going to not use the semi-colon altogether.I am going to test what will happen when I play around with the script.)

To learn more about the NPM package "Prompt", click here.

Wednesday, August 22, 2012

Playing with javascript

I find myself facing a wall with javascript/node.js.

I went through the basics of javascript today. Except for the syntax (grammar) and a few differences in how processes are made, they are pretty much the same. Almost.

I decided to test what I have learned by following a python tutorial on how to create games. In the first chapter, there was a line in the script where the program asks for user input.

With ruby, we use gets.chomp. In javascript, there is ,code>prompt, but I couldn't find a way to use prompt with console.log.

I could have used alert or something that's similar, but I did not want to have to run the script through a web browser. That's what node.js was for, so I can run javascript without a browser.

I found a module called prompt, but it's a few lines long, and I'd have to install the module so I can run it. Why can't I just ask for user input through the node.js REPL?

I found a nodejs web framework that seems to be similar to ruby's Sinatra.

Here too, I found myself facing a wall. It wasn't that easy to work as Sinatra was. Maybe it's because nodejs is low-level(-ish) type of language... not like C or C++, but surely not like python or ruby. I did find a few tutorials on how to create a simple blog.

Tuesday, August 21, 2012


Shifting focus, again.

I just saw the introductory video to nodejs on"Node.js First Look".

I am impressed. I am going to spend more time now learning javascript, move up to nodejs soon as I am finished with the basics.

Monday, August 20, 2012

Sinatra Datamapper Postgresql Heroku

I am about to add more toys to my heroku web app in the coming days.

By default, ruby non-rails apps deployed on heroku do not have a database addon (read this), as heroku isn't sure what type of database a developer might require.

Reading their documentation, I was able to get the postgresql addon by entering this command...
j@ime:~$ heroku login
Enter your Heroku credentials.
Password (typing will be hidden): 
Authentication successful.
j@ime:~$ heroku addons:add heroku-postgresql
 !    No app specified.
 !    Run this command from an app folder or specify which app to use with --app 
j@ime:~$ cd aaa
j@ime:~/aaa$ heroku addons:add heroku-postgresql
Adding heroku-postgresql on js812... done, v15 (free)
Database has been created and is available
Use `heroku addons:docs heroku-postgresql` to view documentation.

I am thinking of using datamapper. It's an application that will help my Sinatra app connect to and access an SQL database of my choice--postgresql. To learn more about datamapper, I am reading documentation from their website, as well as looking under the hood of some apps that use datamapper.

I don't know how to use an SQL database at the moment. I have a MySQL tutorial lined up on my account with

Sunday, August 19, 2012

Ruby Attribute Methods

I often get confused with these three concepts:

After watching Ruby Essential Training from, here's what I understand about them now. Let's address attr_writer first.


Use attr_writer when you want ruby to get your input to what this attribute (qualities of a particular object) will be. Here's an example.
j@ime:~$ irb
irb(main):001:0> class Car
irb(main):002:1> attr_writer :color
irb(main):003:1> def color
irb(main):004:2> @color
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> volks =
=> #
irb(main):008:0> volks.color = 'blue'
=> "blue"
irb(main):009:0> puts volks.color
=> nil

In the example above, all Cars have colors, but different cars will have different colors, don't they? By not defining any specific color inside class Car, you then have a say as to what color your car is going to be.

If I did not create a method setter (irb(main):003:1> def color) for color, I get an error message, saying 'color' is undefined.
j@ime:~$ irb
irb(main):001:0> class Car
irb(main):002:1> attr_writer :color
irb(main):003:1> end
=> nil
irb(main):004:0> volkswagen =
=> #
irb(main):005:0> volkswagen.color = "blue"
=> "blue"
irb(main):006:0> puts volkswagen.color
NoMethodError: undefined method `color' for #
 from (irb):6
 from /usr/bin/irb:12:in `
In a way, input for attribute 'color' is coming from the user. You will have to assign the user's input into a "container"--an instance variable (@color), as this is something that def color is going to use.


On the other hand, if you want all cars (class Car) to be of one color, then you use attr_reader instead. I asked Ruby for a 'red' audi, but she gave me an error message instead...
irb(main):001:0> class Car
irb(main):002:1> attr_reader :color
irb(main):003:1> def color
irb(main):004:2> @color = 'blue'
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> audi =
=> #
irb(main):008:0> audi.color
=> "blue"
irb(main):009:0> puts audi.color
=> nil
irb(main):010:0> audi.color = 'red'
NoMethodError: undefined method `color=' for #
 from (irb):10
 from /usr/bin/irb:12:in `


attr_accessor on the other hand, is something else, and I don't quite get it. Take a look...
irb(main):001:0> class Car
irb(main):002:1> attr_accessor :color
irb(main):003:1> def color
irb(main):004:2> @color = "white"
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> ford =
=> #
irb(main):008:0> ford.color
=> "white"
irb(main):009:0> puts ford.color
=> nil
irb(main):010:0> ferrari =
=> #
irb(main):011:0> ferrari.color = "red"
=> "red"
irb(main):012:0> puts ferrari.color
=> nil

I think the reason ruby is insisting that ferrari's color is white (even if I say that it's red) is because I defined @color as white inside of class Car.

In a sense, the script is saying, "You can get any color you want, as long as it's white."

If we do not do a def color @color inside the class definition, you can get any color you want...
irb(main):001:0> class Car
irb(main):002:1> attr_accessor :color
irb(main):003:1> end
=> nil
irb(main):004:0> ford =
=> #
irb(main):005:0> ford.color = 'yellow'
=> "yellow"
irb(main):006:0> puts ford.color
=> nil
irb(main):007:0> porsche =
=> #
irb(main):008:0> porsche.color = 'silver'
=> "silver"
irb(main):009:0> puts porsche.color
=> nil


I am thinking of using this image to go with my 404 page.

I love mondays. Wife is in the office, and my four kids are in school. Although there are a number of house chores that needs to be done, I can choose to do them later on.

I usually go to the gym some time after 11am. If I remember correctly, the gym is going to be closed around that time. This means, I can stay home and continue with my programming education.

For today's workout, I can do yoga around lunch time, then do an hour's brisk walk before dinner.

I was thinking of pointing this domain name to my heroku app, but changed my mind--too much effort. It might take an hour to get things working... I'd rather go to and learn something new.

Right now, I am watching the ruby essentials module. I can't wrap my mind around the classes module. I am going to see that again, and while I am doing that, will write down notes and do the exercises. I learn better when I experience the process, than just watching the video.

syntax on

call pathogen#infect()
"any plugins to install can be extracted to a subdirectory under 
".vim/bundle and will be added to the runtime path.

"the indent script for haml sets expandtab for you automatically 
"when VIM has detected the current file is in haml format. 
"The script is located at $VIMRUNTIME/indent/haml.vim 'setlocal 
"autoindent sw=2 et. This line disables it...
au! FileType haml set noet

set t_Co=256
" changes terminal to 256 colors?

set background=dark

set scrolloff=999

set gfn=Monaco\ 12 "Courier\ 10\ Pitch\ 12
" if has ('gui running')
"    set guifont=Monaco\ 12 Courier_10_Pitch_12
" endif
" default font

if has("gui_running")
  colorscheme jim "vividchalk wombat mustang blackboard
" Remove Toolbar
  set guioptions-=T
  colorscheme wombat256mod "blackboard wombat Mustang
" test code--otherwise, colorscheme blackboard
" set default to blackboard
" also inkpot, gardener, desert256

set showcmd
" this shows what you are typing

set showmode
" should show mode below

set foldmethod=indent
" fold based on indent

set foldnestmax=10
" deepest fold is 10 levels

set nofoldenable
" don't fold by default

set foldlevel=1
" za toggles folding; zM fold all; zR unfold all; :help(folding)

set autoindent
set tabstop=2
set expandtab
set shiftwidth=2

filetype on                   " Enable filetype detection
filetype indent on            " Enable filetype-specific indenting
filetype plugin on            " Enable filetype-specific plugins

set number

nore ; :
nore , ;

set ignorecase
set incsearch

" added july 15, 2011
set nobackup
set nowb
set noswapfile
set autochdir " always switch to the current working directory
set backspace=indent,eol,start " make backspace more flexible

set encoding=utf-8
" set default file encoding to utf-8

set modifiable
" disables read-only protection 

inoremap jj 

" this script is supposed to get gvim to open in previous window position
" where it was closed.
if has("gui_running")
  function! ScreenFilename()
    if has('amiga')
      return "s:.vimsize"
    elseif has('win32')
      return $HOME.'\_vimsize'
      return $HOME.'/.vimsize'

  function! ScreenRestore()
    " Restore window size (columns and lines) and position
    " from values stored in vimsize file.
    " Must set font first so columns and lines are based on font size.
    let f = ScreenFilename()
    if has("gui_running") && g:screen_size_restore_pos && filereadable(f)
      let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
      for line in readfile(f)
        let sizepos = split(line)
        if len(sizepos) == 5 && sizepos[0] == vim_instance
          silent! execute "set columns=".sizepos[1]." lines=".sizepos[2]
          silent! execute "winpos ".sizepos[3]." ".sizepos[4]

  function! ScreenSave()
    " Save window size and position.
    if has("gui_running") && g:screen_size_restore_pos
      let vim_instance = (g:screen_size_by_vim_instance==1?(v:servername):'GVIM')
      let data = vim_instance . ' ' . &columns . ' ' . &lines . ' ' .
            \ (getwinposx()<0?0:getwinposx()) . ' ' .
            \ (getwinposy()<0?0:getwinposy())
      let f = ScreenFilename()
      if filereadable(f)
        let lines = readfile(f)
        call filter(lines, "v:val !~ '^" . vim_instance . "\\>'")
        call add(lines, data)
        let lines = [data]
      call writefile(lines, f)

  if !exists('g:screen_size_restore_pos')
    let g:screen_size_restore_pos = 1
  if !exists('g:screen_size_by_vim_instance')
    let g:screen_size_by_vim_instance = 1
  autocmd VimEnter * if g:screen_size_restore_pos == 1 | call ScreenRestore() | endif
  autocmd VimLeavePre * if g:screen_size_restore_pos == 1 | call ScreenSave() | endif

Sinatra Rolodex App with DataMapper

I am on the lookout for how I can use Sinatra with a database. I found this script from > Sinatra. The author claims it's a rolodex app written in Sinatra, using DataMapper.

Here is the link to the article... Creating Database-driven Apps with Sinatra -

I am posting this here so I can play with it, to see how I can make it work for me.

#!/usr/bin/env ruby
 require 'rubygems'
 require 'sinatra'
 require 'haml'
 require 'dm-core'
 DataMapper.setup( :default, "sqlite3://#{Dir.pwd}/rolodex.db" )
 # Define the model
 class Contact
   include DataMapper::Resource
   property :id, Serial
   property :firstname, String
   property :lastname, String
   property :email, String
 # Show list of contacts
 get '/contacts/' do
   haml :list, :locals => { :cs => Contact.all }
 # Show form to create new contact
 get '/contacts/new' do
   haml :form, :locals => {
     :c =>,
     :action => '/contacts/create'
 # Create new contact
 post '/contacts/create' do
   c =
   c.attributes = params
 # Show form to edit contact
 get '/contacts/:id/edit' do|id|
   c = Contact.get(id)
   haml :form, :locals => {
     :c => c,
     :action => "/contacts/#{}/update"
 # Edit a contact
 post '/contacts/:id/update' do|id|
   c = Contact.get(id)
   c.update_attributes params
   redirect "/contacts/#{id}"
 # Delete a contact
 post '/contacts/:id/destroy' do|id|
   c = Contact.get(id)
   redirect "/contacts/"
 # View a contact
 # TODO: Put at bottom?
 get '/contacts/:id' do|id|
   c = Contact.get(id)
   haml :show, :locals => { :c => c }
 @@ layout
     %title Rolodex
     = yield
     %a(href="/contacts/") Contact List
 %h1 Create a new contact
 %form(action="#{action}" method="POST")
   %label(for="firstname") First Name
   %input(type="text" name="firstname" value="#{c.firstname}")
   %label(for="lastname") Last Name
   %input(type="text" name="lastname" value="#{c.lastname}")
   %label(for="email") Email
   %input(type="text" name="email" value="#{}")
 - unless == 0
   %form(action="/contacts/#{}/destroy" method="POST")
     %input(type="submit" value="Destroy")
     %td First Name
     %td= c.firstname
     %td Last Name
     %td= c.lastname
     %td Email
 %a(href="/contacts/#{}/edit") Edit Contact
 %h1 Contacts
 %a(href="/contacts/new") New Contact
   - cs.each do|c|
       %td= c.firstname
       %td= c.lastname
         %a(href="/contacts/#{}") Show
         %a(href="/contacts/#{}/edit") Edit

Saturday, August 18, 2012

RVM issues

I've seen a number of recommendations on the merits of using RVM to install ruby. This got me interested and I installed RVM on my other pc to upgrade its version of ruby.

Today, while learning about Padrino, I kept getting errors. Looking to google for a solution, I found that the error had something to do with RVM... that I should do this, or do that.

This is the second time that I am having trouble with RVM. The last time I tried to fix it, I ended up reinstalling ubuntu. I don't want to go there again. I decided to remove RVM, and start from scratch.

To remove RVM, I typed rvm implode on my terminal. I then made sure that I removed that short script at the end of my .bashrc that I inserted after installing RVM.

To upgrade and install ruby, I went to and downloaded the latest stable version. After that, on my terminal, I ran...
tar -zxvf ruby-1.9.3-p194.tar.gz
make && sudo make install

Sinatra To Padrino

5PM - At the moment, I have learned how to serve static pages with the Sinatra web framework. I need to be able to use a database with sinatra... but how?

I am thinking of either moving everything into Padrino, which is another Ruby web framework, or use Padrino modules/helpers with my Sinatra app.

Building a web app with Sinatra while learning Ruby on Rails, I get to understand some of the Ruby on Rails concepts better. I am able to extend some of my Sinatra web app by adapting what I learned from Ruby on rails. What I can't get through right now is how to use a databse with Sinatra.

Padrino is Sinatra, taken to the next level. I'll create a web app with Padrino on one pc, and I will continue development with Sinatra on the other PC. This way, I can compare, and learn more about building ruby web frameworks.

6.30PM - EDIT:

I have been playing around with the padrino framework in the last few hours. I am running the framework in the j@ime machine--same as where I am running the RoR and Sinatra frameworks.

Padrino is between Ruby on rails and Sinatra... Rails is big and almost complete, while sinatra has the bare essentials. Learning Padrino, I am going see the difference between these three frameworks.

I have been following the padrino sample blog tutorial. This is how the padrino admin panel looks like...

...and how the sample blog, with one post looks like

The next question now is, will it be difficult deploying this on heroku?

I will finish this sample blog tutorial, have dinner, then see how I can deploy this later.

8.05PM - Reading further into the sample blog tutorial, I found this...

Padrino documentation shows that it is relatively easy to deploy to heroku. It follows the same process that I used to deploy my Sinatra app--except that with Padrino, I need to configure the database.

Not bad, isn't it?

Here's what's going to happen now.

I am going to find the difference between my Sinatra app and this Padrino app, then learn how I can use, and incorporate the Padrino standalone modules with my Sinatra app.

Coffeescript typeof

The typeof operator, produces a string value naming the type of the
value you give it.
coffee> typeof 4.5

coffee>typeof "cat"

Source -- Smooth Coffeescript, Web Optimized

Friday, August 17, 2012

Coffeescript to javascript bare

I learned something new today.

I noticed that when I run coffee -p, the javascript printout on my console is wrapped in a function.

Let me give you an example. Create a coffeescript (name it, say, with the following...
console.log 'hello world'
Now, if I run coffee -p, I get...
(function() {

  console.log('hello world');

At this time, I am not sure if I can use that script as is when I insert it in my sinatra application. Fortunately, I found that if I run the file using

coffee -pb

I would get a more one to one translation of coffeescript into javascript output

console.log('hello world');

The "-pb" flag means, -print, and -bare. This tells coffee to give me the javascript equivalent of the .coffee file, and to not wrap the js output with a function.

I got this idea from the book, CoffeeScript: Accelerated JavaScript Development by Trevor Burnham.

Did I get that right? If not, feel free to correct me in the comment section below. Thanks! FREE Trial

How to get one?

I'm not sure how, but I went to's sign up page the other day when I was still undecided. I filled in the required information, but when it came time to give them my credit card information, I hesitated and closed the browser window.

I am thinking that, somehow, they were able to keep my email address. Seeing that I went as far as the sign up page, they knew I was interested, and thought, "why not let them have a 7-day free trial of our service?"

IMO, that is good marketing. Ruby On Rails 3 Essential Training

I am following's tutorial, Ruby on rails 3 essential training.

The video had me create a new rails app with rails new simple_cms -d mysql

What this does is to create a new rails app, specifying mysql as the default database. Other RoR tutorials that I have followed does not specify -d mysql, and uses SQLite3.

I then run rails generate controller demo index to get a controller and views. This time though, after I run rails s on my terminal, then go to localhost:3000/demo/index, I get this error message.

According to the instructional video, this error had something to do with rails 3 configuration.

I did not get this error when following a different Ruby on Rails Tutorial. I think this error has more to do with mysql. To fix this issue, I have to create a database for this app in mysql, then make sure the app and the mysql database are "talking to each other".

Going back to the terminal, I type mysql -u root -p to access mysql.

mysql> show databases;
| Database           |
| information_schema |
| mysql              |
2 rows in set (0.00 sec)

mysql> create database simple_cms_development;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
| Database               |
| information_schema     |
| mysql                  |
| simple_cms_development |
3 rows in set (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON simple_cms_development.*
    -> TO 'simple_cms'@'localhost'
    -> IDENTIFIED BY 'mypassword'
    -> ;
Query OK, 0 rows affected (0.02 sec)

mysql> SHOW GRANTS FOR 'simple_cms'@'localhost';
| Grants for simple_cms@localhost                                                                                   |
| GRANT USAGE ON *.* TO 'simple_cms'@'localhost' IDENTIFIED BY PASSWORD '*7FA9214D65F7EE1E3E91DC122D9EF345C09EF9F0' |
| GRANT ALL PRIVILEGES ON `simple_cms_development`.* TO 'simple_cms'@'localhost'                                    |
2 rows in set (0.00 sec)

To test if this works, I log out of mysql, then log back in again...

mysql> exit
j@ime:~/arails/simple_cms$ mysql -u simple_cms -p simple_cms_development
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 46
Server version: 5.1.63-0ubuntu0.11.10.1 (Ubuntu)

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Empty set (0.00 sec)

The tutorial then had me go back to the application's root folder, in this case, ~/arails/simple_cms, then I type in rake db:schema:dump.

This command, if I understood correctly, connects this rails app to the mysql database that I created.

Before I do rails s, I go into app/config and open routes.rb. On the second line, I add...
get 'demo/index' do
end the file and close it, cd back to the app's root folder, then run rails s. On the navigation bar, I type in ,code>localhost:3000/demo/index

I now have...

It is now 11.51pm. Time for bed.

Thursday, August 16, 2012

Paid Membership

OK... I am sold. I didn't find videos like these with the other online learning sites. I am going to sign up with

How to become a better programmer | tutorial - YouTube

Resources For Continuing Education

I am seriously considering signing up for paid membership in one of the online programming tutorial or


Video libraries. I learn faster watching than reading books or websites. has a lot of videos for the stuff that I am looking for: ruby, ruby on rails, CSS, HTML5, Javascript, and C++. Teamtreehouse on the other hand, is more focused on web development.

They both offer the same monthly membership--$25. In fact, I already withdrew money from my poker account. The funds are going to be available next week.

What's holding me back is "information overload". I have a lot of ebooks that I can go through on my computer. There are several youtube videos that I still want to see. At the moment, I am concentrating on ruby + its web frameworks (RoR and sinatra), coffeescript and HAML/SASS.

I'm afraid that if I signed up with, say, I'd end up spoon-feeding myself with those tutorials instead of following my interests/curiosity and learn-by-doing.

I don't think I "need" to sign up for membership. There are a lot of resources on the internet and they are for FREE. All I need to do is to be specific as to what I am looking for, and use Google.

My homework for now, are the following:
  • Finish the three coffeescript ebooks that I have
  • Finish Agile web dev book, learning Sinatra as I learn RoR
  • Convert python games into Ruby
  • Use HAML and SASS along the way

Here's what I'll do. I am going to finish these assignments no matter how long it takes. Soon as I finish them, I'll sign up for paid membership and see if I am missing out on anything.

Let's see what happens.

Create HAML Hyperlinks In Sinatra

I spent an hour trying to figure out how to create a link, from one page to another, using haml in Sinatra. I found this solution in stackoverflow that worked for me.

Here is how my index.haml looks like...
  This website runs on Sinatra (a Ruby web framework) and HAML.
  It's going to be some sort of blog that I am going to update on a regular basis. Stay tuned!

  It is now 
  = time

  This link connects to the 
  %a{:href => '/contents'} table of contents
  table of contents.
  This is another link that goes to the <a href="/about">about</a> page.

There are two ways to create hyperlinks using HAML that I am now aware of:
  1. %a with all the whitespace, or,
  2. if I want to keep my code more compact and readable, I use the traditional a href way.
Here's how the web app looks now...
Click on the image to enlarge
Apparently, not all that is HAML is good. Compared to erb and other templating software that I have seen so far. Since HAML is flexible enough for me to use html tags, I am going to stick with HAML until something better comes along. Read this on your free time: Haml Sucks for Content -

EDIT... I tried using Rails' link_to, but couldn't make it to work. I am thinking that, if I can make a method with it, then I can use it somehow. I'll look into this in the future.

Wednesday, August 15, 2012

Configure Woosh Modem

I am switching broadband providers at the end of the month.

I have two providers, as one of my businesses needed backup broadband connection. Since everything is now running on cruise control, we don't need the second one.

I am making this blog post to remind myself how to configure a woosh wireless modem on ubuntu.

Fire up the terminal, type sudo pppoeconf. Follow the on-screen instructions--basically, enter username and password, then OK everything else.

RoR to Sinatra

I mentioned in an earlier post that I was going to finish a Ruby on Rails Tutorial. This way, I can get paid helping clients from odesk as I expand my knowledge about ruby web frameworks.

While reading Agile Web Dev with Ruby, I had an insight that I can apply what I am learning about Ruby on Rails to Sinatra (killing two birds with one ruby). It sounded plausible (as Mythbusters would say)... I'd just look up the Sinatra and HAML references to see what changes in the code that I had to make.

On the tutorial, the author showed how to insert server time into the index page. He did this by inserting ruby code on the say_controller.rb file
def hello
  @time =

...then, opening hello.html.erb inserted @time.
<h1>Hello from Rails!<⁄h1>
It is now <%= @time %>

I played around with this on my sinatra app, and after about an hour, here's how I did it.

Web.rb is my controller file. I inserted a function at the bottom...
def time

Opening views/index.haml, I inserted ruby code using haml...
  This website runs on Sinatra (a Ruby web framework) and HAML.
  It's going to be some sort of blog that I am going to update on a regular basis. Stay tuned!
  It is now 
  = time

I learned that, to insert ruby code using haml, first you type the equal sign, followed by the ruby code. In this case, = time, where time is the function that I def'd in web.rb which gives us


An ORM layer maps tables to classes, rows to objects, and columns to
attributes of those objects. Class methods are used to perform table-level
operations, and instance methods perform operations on the individual

ORM libraries map database tables to classes. If a database has a table
called orders, our program will have a class named Order. Rows in this table
correspond to objects of the class—a particular order is represented as an
object of class Order. Within that object, attributes are used to get and set
the individual columns.

In a typical ORM library, you supply configuration data to specify the map-
pings between entities in the database and entities in the program. Program-
mers using these ORM tools often find themselves creating and maintaining
a boatload of XML configuration files.

Source -- Agile Web Development With Rails

Sinatra App On Heroku

As promised in an earlier post, here is a screenshot of the web app I created using this Sinatra/HAML tutorial from nettuts...

There's not much to it. I am still playing with the code, add more pages to it over time. If you want to see the page, here is the link.

This is my web.rb at the moment...
require "sinatra"
#require "bundler/setup"
require "haml"
require 'coffee-script'

get '/' do
  haml :index

get '/about' do
  haml :about

get '/contents' do
  haml :contents

not_found do
  halt 404, 'Page Not Found.'

I decided to comment out require "bundler/setup". I was getting an error--can't remember where. It was either on production, or when I deploy to the heroku server. I think it was the heroku server. The error message had something to do with bundle.

Looking for answers, the shortcut I found said it was better to do bundle install on production (my own pc) before making git commit..., then doing git push heroku master. This way, only the important stuff are on the heroku server. Does that make sense to you?

Even so, you will see that the web app runs even after I comment out require "bundler/setup".

Here is my Gemfile...
source ''
gem 'sinatra', '1.1.0'
gem 'thin'
gem 'haml'
gem 'coffee-script'

...and my Procfile
web: bundle exec ruby web.rb -p $PORT

Tuesday, August 14, 2012

Ruby on Rails Tutorial

I reactivated my odesk contractor account today.

The contractor profile that I used before was active with Internet Marketing, not Web Development.

For me to get good projects in there, I am going to take some of the proficiency tests offered by odesk. I have two lined up in the coming days--ruby on rails test, and ruby general knowledge test.

Before I take those tests, I am going to finish this ruby on rails tutorial by Michael Hartl.
is concise and highly detailed. Michael did not leave any stone unturned in writing this tutorial. He gives code examples, screenshots, expected outcome, et al. I am grateful that he wrote this--it helped a lot in my understanding ruby on rails better.

I will post a screenshot and link to the web app that I'll be deploying on heroku when I finish the tutorial.

RubyGems Upgrade On Ubuntu

Watching a SASS tutorial on youtube, I realized that my rubygems wasn't up to date.

Doing $gem -v on my pc, I get version 1.3.xx. The latest at this time is 1.8.24

Running $ gem update --system gives me an error message...
ERROR:  While executing gem ... (RuntimeError)
    gem update --system is disabled on Debian, because it will overwrite 
the content of the rubygems Debian package, and might break your Debian 
system in subtle ways. The Debian-supported way to update rubygems is 
through apt-get, using Debian official repositories.

If you really know what you are doing, you can still update rubygems by 
setting the REALLY_GEM_UPDATE_SYSTEM environment variable, but please 
remember that this is completely unsupported by Debian.

Going to my good friend, Google, I found this on stackoverflow and gave it a try...
$ sudo gem install rubygems-update
Successfully installed rubygems-update-1.8.24
1 gem installed
Installing ri documentation for rubygems-update-1.8.24...
Installing RDoc documentation for rubygems-update-1.8.24...

$ gem -v

$ sudo update_rubygems
RubyGems 1.8.24 installed

== 1.8.24 / 2012-04-27
* 1 bug fix:
  * Install the .pem files properly. Fixes #320
  * Remove OpenSSL dependency from the http code path
RubyGems installed the following executables:

j@ime:~$ gem -v

SASS Basics

Mastering Sass: Lesson 1 - YouTube

I Am A Writer

I am done playing poker as a business.

I used to play poker, like six hours each day, 6 days a week...averaging 50,000 hands each month. It's not that rewarding anymore.

Sure, I tripled my money in about six months, but my life did not have balance. I felt like I was a slave to pokerstars. As a business, the return on my investment was good, but it's not on autopilot.

I learned a lot of things though. Because I played poker, I became interested in programming. I learned python, and moved on to ruby and web development, where I am now. I also have dibs on internet marketing and creating web applications.

I don't think I wasted my time playing poker as a business. And now it's time for me to move on.

I logged into my odesk contractor account yesterday--first time in 10 months since I was active in there. I actually forgot what my username was.

The next chapter in my life is going to be getting paid while I improve my programming skills. That is possible in odesk. First, I set up some web apps that potential employers can verify. When they see that you know what you are doing, the next question is, can the afford you.

From here on, I am going to finish the ruby and coffeescript tutorials. While I am doing that, I am going to start looking into getting gigs from odesk.

I'm also going to spend more time writing and updating my internet marketing websites. One of my sites is starting to make money. I can apply the lessons I have learned making that one site work with all my other websites.

I am putting my writing hat on now--copywriting and writing code.

Coffeescript Is Like Ruby?

Here is one reason why I find it easy to learn javascript using coffeescript. Here's the script in js. If you were NOT a programmer, would you know what this script will do?

(function() {
  var giveWork, person;

  person = {
    name: "Jim",
    job: "Programmer"

  giveWork = function(person) {
    switch (person.job) {
      case "Programmer":
        return console.log("Here's your code work " +;
      case "Designer":
        return console.log("Here's your design work, " +;
        return console.log("Do you work here?");



Now here is the same script written in coffeescript. Read each line out loud and see if you can get an idea as to what this script will do...

person =
  name: "Jim"
  job: "Programmer"

giveWork = (person) ->
  switch person.job
    when "Programmer"
      console.log "Here's your code work #{}"
    when "Designer" then console.log "Here's your design work, #{}"
      console.log "Do you work here?"

giveWork person

...and it reads like Ruby, doesn't it?

Parse Error: Unexpected POST_IF

Following a coffeescript tutorial from nettuts, I encountered this error after entering the following on REPL...
coffee> name = "jim"
coffee> if name is "jim"

Soon as I hit enter after typing the second line, i got these error messages...
Error: In repl, Parse error on line 1: Unexpected 'POST_IF'
    at Object.parseError (/usr/lib/node_modules/coffee-script/lib/coffee-script/parser.js:477:11)
    at Object.parse (/usr/lib/node_modules/coffee-script/lib/coffee-script/parser.js:554:22)
    at exports.compile.compile (/usr/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:43:20)
    at Object.exports.eval (/usr/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:140:10)
    at run (/usr/lib/node_modules/coffee-script/lib/coffee-script/repl.js:131:41)
    at ReadStream. (/usr/lib/node_modules/coffee-script/lib/coffee-script/repl.js:173:9)
    at ReadStream.EventEmitter.emit (events.js:88:17)
    at TTY.onread (net.js:395:14)

If you know how I can fix this, kindly let me know by using the comment box below.

In the meantime, I am going to google for a solution now. I'll update this post soon as I find a solution.


I was thinking the parse error had something to do with syntax, but I wasn't sure. I decided to download and install the vim plugin, "kchmck / vim-coffee-script" into ~/.vim.

I then ran the script, coffee No more errors!

Weird, but it must have been the whitespace. But how come I was getting the parse error when I ran the script on REPL?

Monday, August 13, 2012

Coffeescript Basics

Here are a few videos that I found off youtube that shows some of the things that you can do with coffeescript...

CoffeeScript Basics -- A Teach Me To Code Tutorial - YouTube

I also had access to some coffeescript videos from envato/ These videos are comprehensive and covers a lot of things about coffeescript. I don't think these are available in youtube.

Comparing Syntax

At this time, I see some similarities between coffeescript syntax and ruby syntax. Here's an example...

I created a script,
name = "jim"
greet = "Hello #{name}"

console.log greet

Running this on my terminal...
$ coffee

Hello jim

To see how the file comes out into javascript, I do...
$coffee -p
...and I get...

(function() {
  var greet, name;
  name = "jim";
  greet = "Hello " + name;

If I were to write into ruby, the script will come out like...
def greet
  name = "jim"
  greet = puts "Hello #{name}"


See what I'm doing here?

I am teaching myself web development using ruby. At this time, I am learning Sinatra and now have a simple blog up running in heroku. So that I can spiffy up that blog, I am learning html5, css (SASS, or LESS), and javascript via coffeescript.

I am intimidated to learn javascript as is. I got spoiled with ruby syntax. I don't want to bore myself having to learn js with all those curly braces and semi-colons. As a workaround, I am teaching myself javascript by way of coffeescript, which, I think, is similar to ruby.

Updating Coffeescript

I realized that the coffeescript version I was using wasn't up to date. I have OCD with issues like this and decided to do something about it.

On my terminal, I typed in...
sudo npm install -g coffee-script
... and got a series of error messages, one of which was...
npm ERR! error installing coffee-script@1.1.2 
Error: Refusing to delete: /usr/local/bin/coffee 
not in /usr/local/lib/node_modules/coffee-script
What I understood from this was that, coffeescript was installed in a different directory, /usr/local/bin/coffee, and the new install wanted to install at /usr/local/lib/node_modules/coffee-script

Google is the programmer's best friend. When I am faced with situations like these, I look it up. The solution that worked for me was...
sudo npm install -g coffee-script --force

What happened was that I gave permission to the machine to do what had to be done to install coffeescript ( --force)... and that (I think) deleted the /usr/local/bin/coffee, then installed coffeescript into /usr/local/lib/node_modules/coffee-script

Now, my pc is running Coffeescript v 1.3.3
$ coffee -v
CoffeeScript version 1.3.3

I have been in front of the computer since 9am. It is now 12.48pm. Time for a break--YOGA time!

New Blog Name

After finding a good book to learn Coffeescript last night, I decided to register a domain name for this

I was thinking of something more general and in line with coding, maybe "" but it was too "out there". It wasn't specific. If your site is not that specific, you are missing out on some SEO juice.

Nobody goes to google to look for programmers. People look for programmers for a specific language.

With a more subject-specific blog or website, when someone wants to find information about ruby programming, then this site gets points getting found. If they were to narrow down their search to "ruby + coffeescript", then this blog gets more points, creeping higher up the search engine results page (SERP).

As you know, the higher your site shows on these SERPs, the more traffic that you get. * I got into web development as an off-shoot being an internet marketing copywriter. As such, I know a few things about making it easy for people to find your website on the internet.

I'm not sure what I am going to do with this blog at the moment. All I know is that I had this insight to register the domain and keep the "coffee with my ruby" tag line. I'm the kind of guy who takes his insights seriously.

For now, I use this site for my own documentation. I learn something new about ruby and coffeescript, I'll post them in here. Again, the objective is not to teach, but to share my experience in learning and exploring these programming languages.

Install Coffeescript Ubuntu Software Center

I was trying to follow the install process from the coffeescript website, several tutorial websites, as well as the pragmatic bookshelf's book on coffeescript. I kept getting an error when I try to install npm, and another error when I try to install coffeescript.

Good thing I looked into the Ubuntu Software Center, and there it is!

I simply click on the install button, and I have coffeescript on my pc in less than a minute. I fired up my terminal to see which version I have-- version 1.1.1

To further verify this, I fired up the terminal, then typed in "node". This takes me to the Node.js REPL, an environment where you can interactively run commands. Now enter... require('coffeescript'):
$ node

> require('coffee-script')

Which gave me this output--
> require('coffee-script')
{ VERSION: '1.1.1',
   [ 'case',
     'off' ],
   { starts: [Function],
     ends: [Function],
     compact: [Function],
     count: [Function],
     merge: [Function],
     extend: [Function],
     flatten: [Function],
     del: [Function],
     last: [Function] },
  compile: [Function],
  tokens: [Function],
  nodes: [Function],
  run: [Function],
  eval: [Function] }

The book, Pragmatic Bookshelf, Coffeescript tells me that if you get this output, everything is A-OK.

Sunday, August 12, 2012

Deploy Sinatra To Heroku

I tried to follow the examples given in this tutorial, but somehow they did not work for me. Using google, I had to make some changes.

Here's documentation of what I did to make it work.

First, I log in to the heroku server...
$heroku login
Enter your Heroku credentials.
On my computer, I cd into my working directory (/home/j/aaa/), then created a ruby file, web.rb...
require "sinatra"
require "bundler/setup"

get '/' do
  "Hello, world"
I then created my Gemfile, as this was required by heroku, with the following content...
source ''
gem 'sinatra', '1.1.0'
gem 'thin'
One more file that was needed to deploy my Sinatra app with heroku was a Procfile.

A Procfile is a script for a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform. It follows the process model on the large scale of the Heroku dyno manifold. You can use Procfile to declare any other process types, such as a multiple types of workers, or a singleton process like a clock or a consumer of the Twitter streaming API (source - heroku)

"I don't exactly know what that means atm. All I know is that I wouldn't be able to deploy without that script." On my terminal, I type in gvim Procfile, then input the following into that file...
web: bundle exec ruby web.rb -p $PORT

After I have these setup in my working directory (~/j/aaa), on my terminal, I then do...
git init
git add .
git commit -m "init"

If you need to review what these git commands mean, look here--Git Reference

Back to heroku, on my terminal, I type in...
heroku create jims08122012   # => my app name

Creating jims08122012... done, stack is cedar |
Git remote heroku added

And finally...
git push heroku master

Warning: Permanently added the RSA host key for IP address '' to the list of known hosts.
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 688 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)

-----> Heroku receiving push
-----> Ruby app detected
-----> Installing dependencies using Bundler version 1.2.0.rc
       Running: bundle install --without development:test --path vendor/bundle --binstubs bin/ --deployment
       Fetching gem metadata from
       Installing daemons (1.1.9)
       Installing eventmachine (0.12.10) with native extensions
       Installing rack (1.4.1)
       Installing tilt (1.3.3)
       Installing sinatra (1.1.0)
       Installing thin (1.4.1) with native extensions
       Using bundler (1.2.0.rc)
       Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.
-----> Discovering process types
       Procfile declares types -> web
       Default types for Ruby  -> console, rake
-----> Compiled slug size is 1.9MB
-----> Launching... done, v3 deployed to Heroku

Friday, August 10, 2012

Change in priorities

learn ruby lesson plan

I decided to focus less on learning C++, and instead spend more time with Web Development, specifically, using Sinatra, HAML and SASS.

I found this article from nettuts about Sinatra, which got me to look at Catlin's work--HAML and SASS.

These three tools look easier, cleaner and faster to work with than some of the web development tools that I have seen so far.

So the new direction is--learn ruby while learning web development.

Much of the books that I am reading right now seem to have been written for highly technical people--meaning, they are boring to read.

I learn by testing. When I find myself with tech stuff that I don't understand, I fire up irb and test it. If it's a script, I write it down on gvim, then run the code.

From there, I am going to write about these concepts in such a way that my kids will understand. This too, helps me. When I need to look back on my notes some time in the future, it wouldn't be hard for me to understand them.

One more thing... I don't want this blog to be another drab, technical blog. Whenever possible, I will post a picture that I like from the net.

For copyright foobars out there, I do not claim ownership to any of the stuff posted on this blog. If you want me to take them down, or mention source, do let me know.

Introduction to Sinatra - YouTube

Introduction to Haml - YouTube

Github Top 10 Languages

Click on the image to enlarge.

Source -

Thursday, August 9, 2012

Python to Ruby - Guessing game

I taught myself python a year ago.

I did not get too far with it. I learned the basics, went on to django, found myself facing a wall, got frustrated and decided to do nothing about it.

Reading about ruby bores me. I'd rather do something, and learn as I go. I learn better this way.

So, to learn ruby, I am going to get exercises from the book, "Invent your own computer games with python". I will convert the python games from that book into ruby, then post them in this blog.

Here's the second python game code from that book that I converted to ruby.

# This is a guess the number game.

import random

guessesTaken = 0
print 'Hello! What is your name?'
myName = raw_input()
number = random.randint(1, 20)
print 'Well, ' + myName + ', I am thinking of a number between 1 and 20.'

while guessesTaken < 6:

print 'Take a guess.'
guess = raw_input()
guess = int(guess)
guessesTaken = guessesTaken + 1

if guess < number:
print 'Your guess is too low.'

if guess > number:
print 'Your guess is too high.'

if guess == number:

if guess == number:
guessesTaken = str(guessesTaken)
print 'Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!'

if guess != number:
number = str(number)
print 'Nope. The number I was thinking of was ' + number

Here is how I translated it into RUBY...
guessTaken = 0
guessStr = guessTaken.to_s

puts "Hello! What is your name?"
player = gets.chomp

number = rand(20)
num1 = number.to_s
puts "Well #{player}, I am thinking of a number between 1 and 20."

# can make a function here?
while guessTaken < 6
  puts 'Take a guess.'
  guess = gets.to_i
  guessTaken = guessTaken + 1

  if guess < number
    puts "Your guess is too low."
  elsif guess > number
    puts "Your guess is too high."

if guess == number
  puts "Good job #{player}! You guessed my number in #{guessStr} guesses!"

if guess != number
  puts "Nope. The number I was thinking of was #{num1}."

Looking at the two codes above, one might say that python code looks cleaner and only takes less lines to do the job. I agree. Python code takes up less space, as blocks do not have to have "end" to them.

In my experience though, I find it easier to understand ruby. There's a lot of good python tutorials out there, and I must say that some of these are better written than the ones I find with ruby.

But ruby seems easier for me to understand--at my level. I don't know if this is going to be so the more I go deeper into ruby. But looking at the syntax, and how I it is easier for me to understand the language just by looking at it, I am going to stick with ruby.

EDIT(Nov. 2, 2012):
I made changes to this game's code. Here it is...
puts "#{myName}, I am thinking of a number between 1 and 6. Guess what it is."
myNum = rand(1..6)
tries = 0

while tries < 6
    puts 'Take a guess...'
    guess = gets.chomp
    a = guess.to_i
    tries = tries + 1

    if a < myNum
        puts "#{guess} is too low. Try again."
    elsif a > myNum
        puts "#{guess} is too high. Try again."
    else a == myNum
        puts "Great! You guessed my number in #{tries} attempts."