From: Ruby Mania Date: 2012-04-03T06:05:49+09:00 Subject: Re: How to access Class variable? Hi Jan, If I do test=Test.new , it will ask me to pass the parameters (name,category) but I dont wish to do that when I am accessing the values. Anyother input will be useful Thanks ! Jan E. wrote in post #1053720: > Hi, > > Ruby Mania wrote in post #1053698: >> ***How do I access the the object values. >> eg: test.name should give Hello and test.category should give 2 > > This is exactly what you have to write: test.name and test.category > (assuming you did test = Test.new ... before) > > Ruby Mania wrote in post #1053698: >> ***Also if I have to update a value how do I do that >> eg: test.name = "Hellozzz" [change Hello to Hellozzzz] > > Again: This is exactly what to do: test.name = ... > > When you call > > attr_accessor :name, :category > > then Ruby creates getters and setters for @name and @category. To be > more specific: Ruby creates two methods for @name, which are > > def name() > @name > end > > def name=(value) > @name = value > end > > The same for @category. The first method simply returns the value of > @name. If you leave out the parenthesis, the method call looks like > you're accessing an attribute: > > test.name # actually a method call: test.name() > > The second method allows you to change the value of the variable. Note > the "=" in the method name. This allows you to use assignment syntax: > > test.name = 'abc' # actually a method call: test.name=('abc') -- Posted via http://www.ruby-forum.com/.