Showing posts with label info. Show all posts
Showing posts with label info. Show all posts

Sunday, December 30, 2007

Understanding include and extend in Ruby

I was watching a presentation by Dave Thomas called, "MetaProgramming - Extending Ruby for Fun and Profit", and realized that my understanding of include and extend is mixed up.

My Wrong Understanding
I thought "extend" will add methods from a module into a class as instance methods and "include" will add them as class methods.

Correct Understand:
"extend" adds methods from a module into a class as class methods.
"include" adds methods from a module into a class as instance methods.

It is quite easy to demonstrate this actually.

module SomeModule
def hi
puts "hello"
end
end

class ExtendSample
extend SomeModule
end

ExtendSample.hi

class IncludeSample
include SomeModule
end

IncludeSample.new.hi

Monday, July 02, 2007

Build FaceBook Application using Rails

Here are links to two blog articles on how to build FaceBook application using Rails.

From GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS:

fist in your facebook

From Liverail >> Rails with currents:

Tutorial on developing a Facebook platform application with Ruby On Rails

Wednesday, March 14, 2007

Validate a number is an integer and within a certain range

Use validates_numericality_of , and validates_inclusion_of to check that a number is an integer and within a certain range.

For example:

You only want to allow people to enter a number between 21 and 30. Here is how you would do it.

validates_numericality_of :n, :only_integer => true, :message => "can only be whole number."
validates_inclusion_of :n, :in => 21..30, :message => "can only be between 21 and 30."