Reputation: 3893
i have a class method where i want to access the value of an attribute
class Run
attr_accessor :line
def self.output(options={})
station_no = options[:station]
title = options[:title]
line = self.line
station = line.stations[station_no-1]
end
end
within this class method i want to access value of line
attribute and within class method i can't access the value of an attribute using self.line
. So please suggest me how i can access.
Upvotes: 1
Views: 368
Reputation: 12225
Class method is executed in class context and line
is instance method, you can't directly access it from self.output
.
Do you really want to access instance attribute from class method? Maybe what you need is class attribute. If so, you can declare it like this:
class Run
class << self
attr_accessor :line
end
end
, and will be able to get it's value within class method.
If you do need to access instance attribute from class method — pass that instance as argument to method and call accessor on it.
Upvotes: 5