Sunday, November 26, 2006

Yet another Ruby Hello World Program

Trying hello world program once again
 
class Helloworld
    def initialize( name )
        @name = name
    end
 
    def setName(a_name)
        @name = a_name
    end
 
    def getName()
        return @name
    end
end
 
h = Helloworld.new( "Krish" )
puts h.getName()
h.setName( "Aiyana" )
puts h.getName()
 
 
Save the file with any name and extension as *.rb unlike java where top level class name has to be same as file name. Then execute the following command:
 
ruby name.rb
 
NOTE:
 
Do not forget to name the class with first letter as capital letter.
I didnt put the first letter of class name as capital letter and got the following error:
 
hw.rb:1: class/module name must be CONSTANT

0 Comments:

Post a Comment

<< Home