Plis help. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Plis help.

calling the accessor method inside a class using the self keyword. i need a little more explanation. thank u

3rd Feb 2017, 7:55 PM
stephen haokip
stephen haokip - avatar
2 Answers
+ 1
ruby is not like python or other languages, it usually doesnt need a self keyword for calling var, because it use @ to distinguish class var and instense var. self represent the object its self. we use it to call the method in the same class.
18th Feb 2017, 6:24 PM
Steven Wang
Steven Wang - avatar
+ 1
for example, in python: class Box(object): def __init__(self,name): self.name = name ........ in ruby: class Box def initialize(name) @name = name end def foo puts "#@name is fun" end def hoo self.foo end end or, you can : class Box attr_accessor :name def initialize(name) self.name = name end end
18th Feb 2017, 6:30 PM
Steven Wang
Steven Wang - avatar