Hi, Am 31.01.2011 23:40, schrieb Kamarulnizam Rahim: > I have a problem inheriting data from the parent class to its > subclasses. My code is as followed: > > class Environmental > def initialize > convert_yaml = YAML::load_file('nizam.yaml') > end You declare 'convert_yaml' as a local variable here. It 'disappears' as soon as the method is left. Even an instance variables (@convert_yaml) would be invisible to subclasses. And every instance of this class or a subclass would have it's own copy of the YAML file. I don't known if you intend this. If you want that every subclass has it's own copy of the YAML file, then one way to do this, would be to declare 'convert_yaml' as an instance variable. def initialize @convert_yaml = YAML::load_file('nizam.yaml') end and add an accessor to it: def concert_yaml @convert_yaml end An accessor is a method which gives access to an instance variable. Then your following code should work. > class EnergyManagement< Environmental > def initialize(title) > @title = title > end > def display > #convert_yaml = YAML::load_file('nizam.yaml') > convert_yaml["System"]["Environmental"]["children"][2]["children"] > << @title > > File.open("nizam_out.yaml", "w"){|f| YAML.dump(convert_yaml, f)} > end > end > > class WasteManagement< Environmental > def initialize(title) > @title = title > end > def display > #convert_yaml = YAML::load_file('nizam.yaml') > convert_yaml["System"]["Environmental"]["children"][3]["children"] > << @title > File.open("nizam_out.yaml", "w"){|f| YAML.dump(convert_yaml, f)} > end > end > > I want to use 'convert_yaml' for its subclasses (EnergyManagement and > WasteManagement) but when i run my code, 'convert_yaml' is not > recognized in the subclasses. > > undefined local variable or method `convert_yaml' for > #<EnergyManagement:0x261d0b0> (NameError) See comment above. > > The reason i use this inheritance is because i want to write on the same > yaml file if both subclasses were called. Before this, i use > convert_yaml on every subclass but i found out that the yaml output > ("nizam_out.yaml") file was overwritten if i call both subclasses at the > same time. A common super class won't fix your problem. You have still the same behavior. I think what you want is one class instance variable for all subclasses. Or better, factor the file handling out into a single class and initialize WasteManagement and EnergyManagement with an instance of this new class. Greetings Waldemar