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
#########
Output:
#<GroceryStore:0x24446a0 @name=“Hello”, @category=“1”\n">
#<GroceryStore:0x2444598 @name=“Howdy”, @category=“2”>
***How do I access the the object values.
eg: test.name should give Hello and test.category should give 2
***Also if I have to update a value how do I do that
eg: test.name = “Hellozzz” [change Hello to Hellozzzz]
Any help is appreciated
Hi,
Ruby M. 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 M. 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 M. 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 M. 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’)
Ruby M. wrote in post #1054699:
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
Ruby M.,
If you use something like:
attr_accessor :category
Then you get two methods for “free”:
your_object.category
and
your_object.category = ‘some value here’
You can do so on your own, always, as well:
class Foo
def category
@category
end
def category=(input)
@category = input
end
end