+ 1
Plis help.
calling the accessor method inside a class using the self keyword. i need a little more explanation. thank u
2 Antworten
+ 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.
+ 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