Class struggle
-
Hi
Im very new and struggling to access my first lot of class definitions.
Ive scoured some online books but cant find the error. Can anyone spot the problem
class Widget
def initialize(xPos, yPos, zPos)
@xPos = xPos
@yPos = yPos
@zPos = zPos
end
endmyW= Widget.new(1.1,0,8)
puts myW.inspect
puts myW.xPosThe inspect works fine but i cant access the attributes.
Thanks
Paul
-
You haven't defined any attributes. You just define instance variables.
<span class="syntaxdefault"><br />class Widget<br /><br /> attr_accessor</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">xPos</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">yPos</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">zPos </span><span class="syntaxkeyword">)<br /><br /></span><span class="syntaxdefault"> def initialize</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">xPos</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> yPos</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> zPos</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">xPos </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> xPos<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">yPos </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> yPos<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">zPos </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> zPos<br /> end<br /><br />end<br /><br />myW</span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Widget</span><span class="syntaxkeyword">.new(</span><span class="syntaxdefault">1.1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">8</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">puts myW</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">inspect<br />puts myW</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">xPos<br /></span>
-
<span class="syntaxdefault"><br />class Foo<br /> attr_accessor</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">bar </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">end<br /></span>
is the same as
<span class="syntaxdefault"><br />class Foo<br /> </span><span class="syntaxcomment"># Getter<br /></span><span class="syntaxdefault"> def bar<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">bar<br /> end<br /> </span><span class="syntaxcomment"># Setter<br /></span><span class="syntaxdefault"> def bar</span><span class="syntaxkeyword">=(</span><span class="syntaxdefault"> new_value </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">bar </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> new_value<br /> end<br />end<br /></span>
-
ThomThom
thanks so much!
Paul
Advertisement