Ruby tutorial session 2

Defining a Class and Instantiating an Object

class Person  
  def initialize(name)  
    @name = name  
  end  
  
  def say_hi  
    puts "#{@name} says hi"  
  end  
end  
  
andreas = Person.new("Andreas")  
andreas.say_hi

After writing this code in file create filename with .rb extenstion.
In you command line execute the file:


> ruby example.rb 

Andreas says hi 

Just practice this In next post we will learn Class Inheritance.

No comments:

Post a Comment

My Experience in RUBYCONF INDIA 2016

Hi Readers, Rubyconf India is a global event complementing other RubyConf events across the world. I attended this event held on 19-20 ma...