Tuesday, March 20, 2012

Tutorials on ruby

The most intresting language after java is ruby , may be something more than java. Honestly I do not want to hurt the java developer anyway. Here is a below sample program which will give you a sample IDEA of ruby language. It is really a pure object oriented language.

class TestClass
 @@fname=""
 @@lname=""
 @@val="Bhubaneswar"
 
 def initialize(fname,lname)
  @fname=fname
  @lname=lname
 end

 def testMethod(val)
  puts("Inside the Method")
  puts("The Global Value is '#{@fname}' and '#{@lname}'")
  @val=val
  puts("The Local Value is '#{@val}'")
  puts("The Local Value is '#{@@val}'")
 end
end
obj=TestClass.new("khirod","Patra")
obj.testMethod("Delhi")


Description: TestClass is the name of the class must be stared with a capital letter. @@fname,@@lname,@@val are the class variable. The class variab;e declared with @@ symbol and the instance variable declared with single @ symbol. In the method testMethod you can see the instance variable i.e. @val . So if you want to access the class variable or the instance variable , you have to prefix with @@ or @ symbol with variable name, Incase if you have same variable name

Questions are welcome...

No comments:

Post a Comment