Monday, June 21, 2010

Ruby - OOP

Ruby is a 100% object oriented programming language. When i make this statement you may question me "Your now saying the Ruby is a 100% oop language, but you haven't create a even single object in your hello world demo? ". Here is the answer.

puts "Hello World";

When you run the above code a default main object is created on the memory and puts method is invoked with "Hello World" argument.

Now i briefly explain how to create your own class, method and object.

class MyClass
def saysomething
puts("Hello")
end
end

my_class = MyClass.new
puts my_class.class
my_class.saysomething

Not on the code
-----------------
* class declaration started by class MyClass and ends with end.
* methods are declared using def keyword following method name, ends with end.
* Object is created by invoking the new method of the class. new method is inherited from top most parent class.

No comments:

Post a Comment