Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Sunday, 19 February 2012

MongoDB with Rails Getting Started


There are a few documents in Rails website and MongoDb website. Unfortunately they are out of date. Recently I’ve set up a new Rails project with MongoDb. I’ve used following steps:
o    rails project_name -m "http://gist.github.com/219223.txt"
o    Save the following to config/database.rb file
MongoMapper.database = "db_name-#{Rails.env}"
Replace the db_name with your database name.
o    Remove everything from config/database.yml file. This file should be empty.
o    In config/application.rb file replace
require 'rails/all' with following four require statements.
 require "action_controller/railtie"
 require "action_mailer/railtie"
 require "active_resource/railtie"
 require "rails/test_unit/railtie"
o    In config/application.rb file , in the Application class add following line
 config.gem 'mongo_mapper'
o    From environments/development.rb ,  environments/production.rb and  environments/test.rb file remove all reference to active_record
o    Add following entries to the gemfile
gem 'bson_ext'
gem 'mongo_mapper'
o    From test/test_helper.rb file remove  
fixtures :all


Monday, 7 September 2009

Reading Microsoft Office Excel file in ruby without installing MSOffice

There are two ways one can read Excel file in Ruby; first using Win32OLE control of excel. In that case MS office has to be installed in the machine.

Second option is to use parseexcel library. In this case MS office installation is not required. This library is available as standard gem from the ruby forge website.

Simply install the parseexcel by running following command:
gem install parseexcel

To use this library inlcude
require 'parseexcel'

And then a xls file can be loaded by simply calling

@workbook = Spreadsheet::ParseExcel.parse(file_name)

@workbook.worksheet(worksheet_no) will return intended worksheet.

And reading content of a cell is simply:
cell = worksheet.cell(row, col)
if cell != nil
contents = cell.to_s('latin1')
end
And following code spits out all cells of a worksheet:

worksheet.each do |row|
j=0
i=0
if row != nil
row.each do |cell|
if cell != nil
contents = cell.to_s('latin1')
puts "Row: #{j} Cell: #{i}> #{contents}"
end
i = i+1
end
end
end

Friday, 19 December 2008

Ruby operators et el

Table of Content

= 1

== 3

!= 3

=~ 3

!~ 3

< 3

> 3

<= 3

>= 4

eql? 4

equal? 4

%q 4

%Q 4

#{expr} 4

= <<delimiter 4

.. 4

... 5

<=> 5

=== 5

=

Assignment operator, it assigns values on the right side( rValue) to the variable in the left side(lValue).
a = 1
a = b + c
x = Array.new
a = b = c = 1 is equivalent to a= 1, be = 1, and c = 1. i.e. assignment can be chained.
a = (b = 4+2) + 3 »
b = 6, a = 9
Parallel assignment: a, b = 3, 4 » a= 3, b = 4.

Simple sequence of variables on the left hand side gets assigned to corresponding value from the right hand side. Parallel assignment is pretty useful in some scenario. Let’s say a= 2 and b = 3, Now if you want to swap the values, you can do it single parallel assignment as follows:

a, b = 2, 3 » a = 2, b = 3

a, b = b, a » a = 3, b = 2

Splitting Array into multiple variable: using parallel assignment an array elements can be assigned to a list of variables.

a=[1, 2, 3, 4, 5]

b, c = a » b = 1, c = 2

b, *c = a » b = 1, c= [2, 3, 4, 5]

b, c = 123, a » b = 123, c = [1,2,3,4,5]

b, *c = 123, a » b = 123, c= [[1,2,3,4,5]]

b, c = 123, *a » b = 123, c = 1

b, *c = 123, *a » b = 123, c = [1,2,3,4,5]

Nested assignment: The left-hand side of an assignment may contain a parenthesized list of terms. Ruby treats these terms as if they were a nested assignment statement. It extracts out the corresponding rvalue, assigning it to the parenthesized terms, before continuing with the higher-level assignment.

b, (c, d), e = 1,2,3,4 » b = 1, c = 2, d = nil, e = 3

b, (c, d), e = [1,2,3,4] » b = 1, c = 2, d = nil, e = 3

b, (c, d), e = 1,[2,3],4 » b = 1, c = 2, d = 3, e = 4

b, (c, d), e = 1,[2,3,4],5 » b = 1, c = 2, d = 3, e = 5

b, (c,*d), e = 1,[2,3,4],5 » b = 1, c = 2, d = [3, 4], e = 5

==

Equality operator. It compares lvalue with the rvalue and result true if they same false otherwise. This compares value only.

a, b = 2, 3

a == b »false

c = d= 4

c == d »true

e, f= 4, 4.0

e == f »true :Notice, although f is a floating point number the comparison return true as their value same.

!=

=~

!~

<

>

<=

>=

eql?

equal?

%q

General purpose single quoted string

%q/something as a string/ » something as a string

%Q

General purpose double quoted string

%Q{something as a string} »something as a string

%QString with expression #{50*6} »String with replacement 300

#{expr}

A Ruby expression block. This specific sequence used in string to create or format a string, generate value dynamically. It simply implies that the block of code inside the curly braces will get executed and the result will be embedded into the string. This sequence only can be used in double quoted string.

"10 inch = #{ 10*2.54} cm" »10 inch = 25.4 cm

"#{'Ho! '*3} Merry Christmas" »Ho! Ho! Ho! Merry Christmas

= <<delimiter

This is special marker called document to write long text. Anything you write after the document until the delimiter is found is assigned to the string variable.

aString = <<END_OF_STRING

The body of the string

is the input lines up to

one ending with the same

text that followed the '<<'

END_OF_STRING »" The body of the string\n is the input lines up to\n one ending with the same\n text that followed the '<<'\n"

..

Range operator; syntax is m..n implies from m up to n , m and n inclusive. m/n could be number, alphabet or any object that implements range.

1..4 » 1, 2, 3, 4

'a'..'k' » a, b, c, d, e, f, g, h, i, j, k

...

Range operator; syntax is m...n implies from m up to n , m included but not n. m/n could be number, alphabet or any object that implements range.

1...4 » 1, 2, 3

'a'...'k' » a, b, c, d, e, f, g, h, i, j

<=>

Refer to spaceship operator

===

Case equality operator. Used to test equality within a when clause of a case statement. All Ruby class implements this operator. In most cases this implies if the value falls within the range. But it can vary depending on the implementation of the class.

(1..10) === 3 » true

(1..10) ===11 » false

kind = case year

when 1850..1889 then "Blues"

when 1890..1909 then "Ragtime"

when 1910..1929 then "New Orleans Jazz"

when 1930..1939 then "Swing"

when 1940..1950 then "Bebop"

else "Jazz"

end

In the above example === operator used in every when clause to identify the match. Simply comparison === target

Thursday, 18 December 2008

<=>

<=> is spaceship operator of Ruby. This is general purpose comparison operator. It compares two values and returns -1, 0, +1 depending on whether value1 is less then, equal or grater than value2.
value1 <=> value2 => -1 : if value1 < value2
   0 : if value1 = value2 
 +1 : if value1 > value2 

This operator is equivalent to ICompare.Compare( Object x, Object y) methods of .NET library.