What is the Ruby equivalent of the "this" function in Java? -
in java there "this" function points method. there equivalent in ruby? instance, there:
def method this.method end
the equivalent self. implict. self.first_name same first_name within class unless making assignment.
class book attr_reader :first_name, :last_name def full_name # same self.first_name + ", " + self.last_name first_name + ", " + last_name end end when making assignment need use self explicitly since ruby has no way of knowing if assigning local variable called first_name or assigning instance.first_name.
class book def foo self.first_name = "bar" end end
Comments
Post a Comment