class and inheritance. Inherit data

K

Kamarulnizam Rahim

Hi guys,

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
def display
#convert_yaml = YAML::load_file('nizam.yaml')
end
end

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)

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. Sorry if my explanation quite confusing as i do not know how
to explain this more clearly. Any help would be great. Thanks in
advance.

Nizam
 
W

Waldemar Dick

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
 
K

Kamarulnizam Rahim

Hi waldemar,

Thanks for your post. Actually, i dont want every subclasses to has its
own yaml file because the purpose of my program is to produce a sole
yaml file, with all data (@title) from every subclasses included in that
file.

Btw, what do you meant by

'I think what you want is one class instance variable for all
subclasses' ?

can you show a simple example on that? One more thing, i saw on net
there is a way by using '@@xxxx' variable and that variable can be
called outside the class. Is it true?

Thanks!!

nizam
 
W

Waldemar Dick

Hi,

Am 01.02.2011 10:02, schrieb Kamarulnizam Rahim:
Thanks for your post. Actually, i dont want every subclasses to has its
own yaml file because the purpose of my program is to produce a sole
yaml file, with all data (@title) from every subclasses included in that
file.

Btw, what do you meant by

'I think what you want is one class instance variable for all
subclasses' ?

Sorry, I meant a class variable (@@var).
can you show a simple example on that? One more thing, i saw on net
there is a way by using '@@xxxx' variable and that variable can be
called outside the class. Is it true?

class MyYaml

@@yaml = nil

def self.yaml
if @@yaml.nil? then
@@yaml = File.open(...)
end
@@yaml
end

end

access the yaml file with: MyYaml.yaml

Greetings,

Waldemar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top