A really stupid question but I am new to OO ruby
I created a class
···
#######
class Test
attr_accessor :name
attr_accessor :category
def initialize(name,category)
@name=name
@category=category
end
data=[]
data << Test.new("Hello","1")
data << Test.new("Howdy","2")
data.each do |temp|
puts temp.inspect
end
end
#########
***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')
By the way: @name and @category are *instance* variables, they belong to
the instances of the class. Class variables are something completely
different (they belong to the class itself and are shared by the
subclasses).
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')
Hi Jan,
If I do test=Test.new , it will ask me to pass the parameters
(name,category)
That's because you defined the initialize method with these two
parameters. If you don't want to set the values on object creation,
simply change the method accordingly (or leave it out completely).
···
####
class Test
attr_accessor :name, :category
end
test = Test.new
# set @name to 'a' and @category to 'b'
test.name = 'a'
test.category = 'b'
puts test.name
puts test.category
####