KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I think I'm going a bit crazy when trying to understand instance variables in Ruby. My only aim here is to make sure that every object created for a given class has a variable with a predetermined value without writing an initialize method for that class. Something like: class Test @my = [] attr_accessor :my end t = Test.new t.my # I want [] but this shows nil Is it possible to achieve this without touching initialize ? Thanks. EDIT: To clarify, I'm writing some piece of code which will be executed similar to attr_accessor in the sense that it'll add an instance variable to the class in which it is executed. If I write my own initialize , I will end up clobbering the one written by the user.
Tags (comma-separated)
Save Edits
Cancel