Basics of Ruby

Ruby is an object oriented programming Language. It is dynamic in nature. Basically it has no primitive data types. It has no built in supports of Interface as dose Java.

Variable Declaration :

  • local variable–> varname.
  • instance variable–> @varname.
  • global variable–> $varname.
  • class variable–> @@varname.

Class method Definition :

def ClassName. methodName
end

def  self. methodName
end

class << self
def methodName

end
end

Inheritance :

class SubclassName < SuperclassName
end

Ruby code block :

objectName . Method do

|object|  puts(object.value)

end

Name Spaces:

The double colone(::) is Ruby’s namespace resolution operator. The thing to the left must be a class or module and the thing to the right is a constant which is defined in that class or module.

Example :–> Math :: pi.

Attribute accessor methods :

  • Attribute setting method

attr_writer :duration

  • Attribute getting method

attr_reader :name, :artist

Iterator:

“find” method is an iterator. It invokes a block of codes repeatedly.

Hook methods:

Non-abstract methods that can be overridden in the concrete classes of the Template Method pattern are called hook methods. Hook methods permit the concrete classes to choose (1) to override the base implementation and do something different or (2) to simply accept the default implementation.

Example:

class Report

def output_start
end

def output_line(line)
raise ‘Called abstract method: output_line’
end

def output_end
end

end

Now, the hook methods in the following subclass of class “Report” are —

class OutputReport < Report

def output_start

puts ‘Output started’

end

def output_end

puts ‘ End of output’

end

end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: