Monday, June 8, 2009

Differences between Ruby 1.9 and bellow 1.9

Hashes

Before Ruby 1.9.1p0

Key and Value are separated by => that means name => value

day_selection = {
'day1' => 'monday',
'day2' => 'tuesday',
'day3' => 'wednesday',
'day4' => 'thursday',
'day5' => 'friday',
'day6' => 'saturday',
'day7' => 'sunday'
}

p day_selection['day1']


In Ruby 1.9.1p0

Key and Value are separated by: that means name: value

day_selection = {
day1: 'monday',
day2: 'tuesday',
day3: 'wednesday',
day4: 'thursday',
day5: 'friday',
day6: 'saturday',
day7: 'sunday'
}

p day_selection[:day1]


Note: Remove single quote from the name

Hashes

In Ruby 1.9.1p0

Ruby remembers the order in which we are add items to a hash. When we will iterate over the entries, Ruby will return them in that order.

Friday, June 5, 2009

Ruby

Note: This post assume that you have successfully installed Ruby on Rails in your computer.

Ruby as a Calculator

Type following command on your command prompt

irb --simple-prompt

and press "Enter" key from the keyboard. You will received following respond

C:\>irb --simple-prompt

>>

At ">>" command prompt, enter "10+20" and press enter key. You will received following respond

C:\ruby>irb --simple-prompt

>> 10+20

=> 30

>>

A "=>" command prompt symbol showing answer of the "10+20" airthmetic equation. Again ">>" command prompt symbol come so you will use this prompt for new airthmetic equation. Ruby support addition (+), subtraction (-), multiplication (*) and division (/) airthmetic operator. Remaining airthmetic operator I will discuss some time later.

If you want to out of irb then type exit at ">>" command prompt and press "Enter" key from the keyboard.

>>exit

C:\ruby>

In my case I am using these command inside ruby folder at "C" drive, so "C:\>Ruby" prompt comes.

Number Datatypes in Ruby

Try following division arithmetic operator

C:\ruby>irb --simple-prompt
>> 3/2
=> 1
>>

An integer is a whole number, like 3, 6, -5, etc. When you operate using only integers, Ruby will give you an Integer answer. 3/2 is 1.5, but that isn't an integer, so Ruby gives you 1 instead.

But if you try 3.0/2.0 then ? let's try

C:\ruby>irb --simple-prompt

>> 3.0/2.0

=> 1.5

>>

A float is a decimal number, like 3.14, 6.51, 3.0, etc. When you operate using only float, Ruby will give you an float answer. 3.0/2.0 is 1.5

More Arithmetic Operator

Exponent (**)

C:\ruby>irb --simple-prompt
>> 6 ** 2
=> 36
>>

Remainder (%)

C:\ruby>irb --simple-prompt
>> 6 % 4
=> 2
>> 7.1 %2
=> 1.1
>>

Working with Large Number

Thursday, June 4, 2009

Ruby On Rails - Interview Question

What is Ruby?

Ruby is a dynamic, reflective, general purpose object-oriented programming language. Ruby combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan in the early 1990s, and has started to become popular worldwide in the past few years as more English language books and documentation have become available.

What is Rails?

Rails is an open source Ruby framework for developing database-backed web applications.

How can we know ruby version?

Open command prompt and type following command.

C:\>ruby –v

It will display following respond.

ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-mswin32]

How to use Ruby Documentation on command prompt?
Using ri tool

ri tools is a local, command-line viewer for the ruby documentation

To find the documentation for a class, just use following syntax

ri ClassName

Example of if we want to summary of GC class then type

ri GC

If we want information on a particular method then pass method name as a parameter

ri GC::enable

How to create new application using rails command?

rails application name

Open command prompt and type rails emailmarketing

Here emailmarketing is the application name.

What is the difference between p and puts?

puts simply writes string for the program

p add much information inside #< … > like p writes the name of the object’s class, followed by a colon and the object’s unique identifier – a hexadecimal number and more
What is the purpose of to_s?

to_s represent any object in string, that means render any object in string
How to create accessor Methods in Ruby?

Traditional Way (Following way normally we used like in Java)

class Car
def initialize(model, price)
@model = model
@price = Float(price)
end
def model
@model
end
def price
@price
end
end

car = Car.new("Honda Civic", 18000)
puts "Model = #{car.model}"
puts "Price = #{car.price}"


BUT

Ruby provides a suitable shortcut using attr_reader. attr_reader creates Accessor Methods

class Car
attr_reader :model, :price
def initialize(model, price)
@model = model
@price = Float(price)
end
end

car = Car.new("Honda Civic", 18000)
puts "Model = #{car.model}"puts "Price = #{car.price}"
How to set accessor methods in Ruby?

The following way is the ideal way for set accessor method

class Car
attr_reader :model, :price
def initialize(model, price)
@model = model
@price = Float(price)
end
def price=(new_price)
@price = new_price
end
end

car = Car.new("Honda Civic", 18000)
puts "ISBN = #{car.model}"
puts "Price = #{car.price}"
car.price = car.price * 0.75 #discount price
puts "New price = #{car.price}"


BUT

Ruby provides a shortcut way for setting accessor method using attr_accessor

class Car
attr_reader :model
attr_accessor :price
def initialize(model, price)
@model = model
@price = Float(price)
end
end

car = Car.new("Honda Civic", 18000)
puts "ISBN = #{car.model}"
puts "Price = #{car.price}"
car.price = car.price * 0.75 #discount priceputs "New price = #{car.price}"
initialize method are always public? True or False?

False, initialize method are always private
How can we stop alteration to the frozen object?

Using freeze we can stop alteration to the frozen object
Example of
objectName.freeze