I’m writing my first application in Ruby and I had a question.
Here is a sample:
class SomeClass
def initialize(host)
@host = host
end
end
How can I change the value of @host within another method? I have tried using
self.host = "somenewvalue" but that doesn’t work.
Use attr_accessor:
or attr_reader and attr_writer to define methods host(getter) and host=(setter) separately.
UPD: got it wrong. You can access instance variables by adding @ in front of the variable name. So @host=”someothervalue” inside an instance method will work fine.