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