From: Tom Cloyd Date: 2009-02-11T19:50:03+09:00 Subject: Re: simple class question 7stud -- wrote: > Tom Cloyd wrote: > >> class Test >> >> def t1=(val) >> @val = val >> return 99 >> end >> >> def t2=(val) >> @val = val * 2 >> return 99 >> end >> >> end >> > > >> I enter at the command line: >> >> (rdb:1) p test.t2 = 2 >> 2 >> >> I should have gotten 4. >> > > I think you should have got 99. You called the method t2=(), and t2=() > returns 99. > > >> I only ever get back the same number I put in. >> Can someone tell me what the problem is, and what I have to do to get >> test.t2 to do this simple thing I'm asking of it? >> >> > > The problem appears to be that ruby does not respect a return statement > in a setter method. Instead, ruby appears to return the value of the > argument to the setter method--which is not necessarily even the value > assigned to the instance variable: > > class Test > > def t1=(val) > @val = val * 2 > return 99 > end > > def t1 > @val > end > > end > > puts t.t1 = 2 #2 (not 99, and not 4!) > puts t.t1 #4 (as expected) > > Normally, a return statement at the end of a method would be the value > returned to the method call: > > class Test > > def t1=(val) > @val = val * 2 > end > def t1 > @val > end > > def a > @val = "goodbye" > return 99 > end > > end > > t = Test.new > puts t.t1 = "hello" #hello (the arg to the setter method) > puts t.t1 #hellohello (the actual value of @val) > puts t.a #99 (the return value of the method) > puts t.t1 #goodbye (as expected) > > You can test on your own that ruby does respect a return statement in a > getter method. > > >> My other question: the "return 99" business is from Thomas. He doesn't >> explain it. I've been reading about "return" and I cannot account for >> why it's there. It doesn't seem to do any thing. Can someone explain >> THAT. >> >> > > In ruby, a method returns the last expression that was executed in the > method or what is explicitly specified in a return statement. > > > >> Finally, is this general approach I'm taking the best or usual way to >> send data to a class method and get some output? Until now, I thought I >> had to read an accessor variable to get output back, but I'm thinking >> now that that is probably NOT the best way. >> >> > > It depends on what you want to do. If you simply want to set and get an > instance variable's value, using attr_accesor saves you from having to > manually type out the setter and getter methods. However, if you want > your setter or getter method to transform the value of the argument in > some way, then you have to define your own getter and setter methods. > > > >> What I'm wanting to do, say, create a file object which gives me more >> data when I call upon it (it'll read a record and do some processing, >> then pass back the results. Normally I'd use a method, but I'm trying to >> become more "classy" in my programming, hence this effort. I'm basically >> still struggling to find a good reason to use a class at all. So far, >> it's proven far too difficult to get something OUT of a class instance. >> I don't see the point of the bother, yet. I'm hoping some enlightenment >> will arrive soon. >> > > Here is an example of what you can do: > > class MyDataProcessor > attr_reader :file > > def initialize(fname) > @file = File.open(fname) > end > > def file=(fname) > @file = File.open(fname) > end > > def get_data > data = @file.gets > > #process data: > data.chomp.split if data #data=nil at EOF > end > > end > > > dp = MyDataProcessor.new("data.txt") > > while data = dp.get_data > p data #do something with processed data > end > > dp.file.close > > Thanks much for your thoughts. You added some material I didn't see (or haven't yet understood!) in Robert's response. I can see you were as surprised (more or less) as I was by the failure of the "return n" statement to do anything. I never could get it to respond as I had expected. I will study your example. I'm sure it will teach me a few things. Thanks! ~t. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << tc@tomcloyd.com >> (email) << TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~