How does ruby call varz method by using varz[:foo]?
class Component
class << self
def varz
@varz ||= Hash.new
end
end
end
Component.varz[:foo] = 'bar'
puts Component.varz # {:foo=>"bar"}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You cut it in the wrong way.
Component.varzis a method call. To the result of it (which is a hash),[:foo] =applies. The[]and[]=methods are special in that you don’t put a period between the receiver and the method.