Monday 30 May 2016

Defining a Class and Instantiating an Object


Ruby Class:

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.

Back to Ruby Introduction >> click

2 comments:

  1. Waiting for your sessions with more advanced concepts. :)

    ReplyDelete
    Replies
    1. Thanks for giving feedback.
      Sure you will get all advanced concepts one after another.

      Delete

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...