August 20, 2009

My first time with Rails


---------RSpec red/green/refactor
module AccountExampleHelperMethods
def helper_method

end
end

shared_examples_for "all editions" do
it "should behave like all editions" do
end
end
describe "SmallEdition" do
include AccountExampleHelperMethods

helper_method
it_should_behave_like "all editions"
it "should also behave like a small edition" do
end
end
---------
rake db:create ..create database
rake db:drop ..drop database
rake db:sessions:clear ..empty sessions table
rake db:migrate ..run db migrations
rake db:migrate VERSION= ..rollback db
rake rails:update:javascripts ..update javascript libraries
rake routes ...list available routes in application

gem server
script/server
script/console
rake db:migrate
rake db:fixtures:load
rake spec:db:fixtures:load


script/generate rspec_model user
-> create data in spec/fixtures/users.yml
-> write our test in spec/models/user_spec.rb

script/generate rspec_controller users

August 19, 2009

Install/uninstall rails with specific version

sudo gem uninstall rails
sudo gem install -v=2.3.2 rails

sudo aptitude remove rake
sudo aptitude install git-core
sudo gem install rake

August 05, 2009

Mulitple inheritance in RUBY

Normally Ruby is a single inheritance language but you can do multiple inheritance by using Mix-ins which is the module that contains many classes inside or I can separate each classes in each modules as bellow then I include them in to wherever I want.
======================================
module ToolBox
class Ruler
attr_accessor :length
end
def mesure
@length = 10
end
def whatever
....
end
end

module Country
class Ruler
attr_accessor :name
end
def unit
@type = 'meter'
end
def whatever
....
end
end

Sample of Ruby basic

stop_words = %w{the a by on for of are with just but and to the my I has some in}
lines = File.readlines("text.txt")
line_count = lines.size
text = lines.join

# Count the characters
character_count = text.length
character_count_nospaces = text.gsub(/\s+/, '').length

# Count the words, sentences, and paragraphs
word_count = text.split.length
sentence_count = text.split(/\.|\?|!/).length
paragraph_count = text.split(/\n\n/).length

# Make a list of words in the text that aren't stop words,
# count them, and work out the percentage of non-stop words
# against all words
all_words = text.scan(/\w+/)
good_words = all_words.select{ |word| !stop_words.include?(word) }
good_percentage = ((good_words.length.to_f / all_words.length.to_f) * 100).to_i

# Summarize the text by cherry picking some choice sentences
sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|\!/)
sentences_sorted = sentences.sort_by { |sentence| sentence.length }
one_third = sentences_sorted.length / 3
ideal_sentences = sentences_sorted.slice(one_third, one_third + 1)
ideal_sentences = ideal_sentences.select { |sentence| sentence =~ /is|are/ }

# Give the analysis back to the user
puts "#{line_count} lines"
puts "#{character_count} characters"
puts "#{character_count_nospaces} characters (excluding spaces)"
puts "#{word_count} words"
puts "#{sentence_count} sentences"
puts "#{paragraph_count} paragraphs"
puts "#{sentence_count / paragraph_count} sentences per paragraph (average)"
puts "#{word_count / sentence_count} words per sentence (average)"
puts "#{good_percentage}% of words are non-fluff words"
puts "Summary:\n\n" + ideal_sentences.join(". ")
puts "-- End of analysis"
------------------RESULT
121 lines
6165 characters
5055 characters (excluding spaces)
1093 words
18 paragraphs
45 sentences
2 sentences per paragraph (average)
24 words per sentence (average)
76% of words are non-fluff words
Summary:
' The surgeon leaned over the body, and raised the left hand. Think what it is
to be a mother, there's a dear young lamb do. 'The old story,' he said, shaking
his head: 'no wedding-ring, I see. What an excellent example of the power of
dress, young Oliver Twist was. ' Apparently this consolatory perspective of a
mother's prospects failed in producing its due effect. ' The surgeon had been
sitting with his face turned towards the fire: giving the palms of his hands a
warm and a rub alternately. ' 'You needn't mind sending up to me, if the child
cries, nurse,' said the surgeon, putting on his gloves with great deliberation.
She had walked some distance, for her shoes were worn to pieces; but where
she came from, or where she was going to, nobody knows. ' He put on his hat,
and, pausing by the bed-side on his way to the door, added, 'She was a
good-looking girl, too; where did she come from
-- End of analysis

Sample of Ruby basic

stop_words = %w{the a by on for of are with just but and to the my I has some in}
lines = File.readlines("text.txt")
line_count = lines.size
text = lines.join

# Count the characters
character_count = text.length
character_count_nospaces = text.gsub(/\s+/, '').length

# Count the words, sentences, and paragraphs
word_count = text.split.length
sentence_count = text.split(/\.|\?|!/).length
paragraph_count = text.split(/\n\n/).length

# Make a list of words in the text that aren't stop words,
# count them, and work out the percentage of non-stop words
# against all words
all_words = text.scan(/\w+/)
good_words = all_words.select{ |word| !stop_words.include?(word) }
good_percentage = ((good_words.length.to_f / all_words.length.to_f) * 100).to_i

# Summarize the text by cherry picking some choice sentences
sentences = text.gsub(/\s+/, ' ').strip.split(/\.|\?|\!/)
sentences_sorted = sentences.sort_by { |sentence| sentence.length }
one_third = sentences_sorted.length / 3
ideal_sentences = sentences_sorted.slice(one_third, one_third + 1)
ideal_sentences = ideal_sentences.select { |sentence| sentence =~ /is|are/ }

# Give the analysis back to the user
puts "#{line_count} lines"
puts "#{character_count} characters"
puts "#{character_count_nospaces} characters (excluding spaces)"
puts "#{word_count} words"
puts "#{sentence_count} sentences"
puts "#{paragraph_count} paragraphs"
puts "#{sentence_count / paragraph_count} sentences per paragraph (average)"
puts "#{word_count / sentence_count} words per sentence (average)"
puts "#{good_percentage}% of words are non-fluff words"
puts "Summary:\n\n" + ideal_sentences.join(". ")
puts "-- End of analysis"
------------------RESULT
121 lines
6165 characters
5055 characters (excluding spaces)
1093 words
18 paragraphs
45 sentences
2 sentences per paragraph (average)
24 words per sentence (average)
76% of words are non-fluff words
Summary:
' The surgeon leaned over the body, and raised the left hand. Think what it is
to be a mother, there's a dear young lamb do. 'The old story,' he said, shaking
his head: 'no wedding-ring, I see. What an excellent example of the power of
dress, young Oliver Twist was. ' Apparently this consolatory perspective of a
mother's prospects failed in producing its due effect. ' The surgeon had been
sitting with his face turned towards the fire: giving the palms of his hands a
warm and a rub alternately. ' 'You needn't mind sending up to me, if the child
cries, nurse,' said the surgeon, putting on his gloves with great deliberation.
She had walked some distance, for her shoes were worn to pieces; but where
she came from, or where she was going to, nobody knows. ' He put on his hat,
and, pausing by the bed-side on his way to the door, added, 'She was a
good-looking girl, too; where did she come from
-- End of analysis

August 04, 2009

Ruby scan only useful/key words

It maybe useful for me later to develop English testing system.
======================================
text = %q{Los Angeles has some of the nicest weather in the country.}
stop_words = %w{the a by on for of are with just but and to the my I has some}
words = text.scan(/\w+/)
key_words = words.select { |word| !stop_words.include?(word) }
puts key_words.join(' ')

Hashes in Ruby

It is just only one part of Ruby which I interested in and It is similar to JSON Object in JavaScript.
======================================
people = {
'fred' => {
'name' => 'Fred Elliott',
'age' => 63,
'gender' => 'male',
'favorite painters' => ['Monet', 'Constable', 'Da Vinci']
},
'janet' => {
'name' => 'Janet S Porter',
'age' => 55,
'gender' => 'female'
}
}
puts people['fred']['age']
puts people['janet']['gender']
puts people['janet'].inspect
puts people['fred']['favorite painters'].length
puts people['fred']['favorite painters'].join(", ")

-----------------Result
63
female
{"name"=>"Janet S Porter", "gender"=>"female", "age"=>55}
3
Monet, Constable, Da Vinci
-----------------

August 03, 2009

Update Ruby to version 1.8.7 in Ubuntu

$ sudo apt-get install build-essential libssl-dev libreadline5-dev zlib1g-dev
$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p22.tar.gz
$ tar zxvf ruby-1.8.7-p22.tar.gz
$ cd ruby-1.8.7-p22
$ ./configure --prefix=/usr/local --with-openssl-dir=/usr --with-readline-dir=/usr --with-zlib-dir=/usr
$ make
$ sudo make install

$ which ruby
/usr/local/bin/ruby
$ ruby --version
ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-linux]
$ ruby -ropenssl -rzlib -rreadline -e "puts :success"
success