Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Can't change the value of self

Reply
Thread Tools

Can't change the value of self

 
 
Ralph Shnelvar
Guest
Posts: n/a
 
      07-15-2010
Ok ... I've googled this error and I see stome explanations about usnig destructive methods ...

I don't get it.

Consider

- - - -
class UD_Config
attr_accessor :container_list
attr_accessor :screen_x, :screen_y, :screen_width, :screen_height

@@config_filename = 'config.yml'

def initialize
debugger
if File.exists? @@config_filename
config_yaml_str = IO.read(@@config_filename)
self = YAML::load( config_yaml_str ) # This line seems to be the problem
else
set_defaults
end
end


def write_me
yaml_obj = YAML::dump( self )
File.open(filename,"w") { |f| f << yaml_obj }
end

private
# call this if there is no config file
def set_defaults
screen_x, screen_y, screen_width, screen_height = 30, 30, 600, 500

@container_list =
%w(
c:\abc\def.udd
d:\def\ghi.udd
f:\\ghi\jkl.udd
)
end
end

- - - -


I have been taught that "an object should take care of itself" ... what's the right way to do what I want?


 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      07-15-2010
Ralph Shnelvar wrote:
> I have been taught that "an object should take care of itself" ...


It can take care of itself, it can't turn into someone else

If YAML.load is returning an instance of class UD_Config directly, then
just return that to the user. Make a class method, so they call

res = UD_Config.load(...)

instead of

res = UD_Config.new(...)

Otherwise, YAML.load is returning some other object - possibly a Hash -
with which you wish to initialize your new UD_Config object. So you need
to copy values out of that object into your object's instance variables.

If YAML.load gives an object with instance variables, you can copy them
in a loop: look at 'instance_variables' and 'instance_variable_get' /
'instance_variable_set'
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing self: if self is a tree how to set to a different self Bart Kastermans Python 6 07-13-2008 11:19 AM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) falcon Python 0 07-31-2005 05:41 PM
Re: __autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 2 07-12-2005 03:20 AM
Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code Ralf W. Grosse-Kunstleve Python 16 07-11-2005 09:28 PM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 18 07-11-2005 04:01 PM



Advertisments