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