Hi! Well, I thought this was easy, but for some reason it won''t work... Here is what I want to do: I am working on an API to a large database. The basic mapping is done, but I want to implement some convenience methods that take information for one object and does some calculations with it. The result should be accessible as attribute. So in a simple example: We have an object that has a start and a stop attribute, i.e. columns in the table. Now I want to know the distance, but directly via an readable attribute. Class Something attr_accessor :distance def initialize @distance = self.stop-self.start end end However, if I use my_object.distance, it returns nil... What am I doing wrong? P.S. I also tried def initialize(distance) - but figured that only makes sense if I specifically create a new object, not if the attribute is created from within the class. Didn''t work either, anyway. Cheers, Marc -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 17, 7:32 am, Marc Hoeppner <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi! > > Well, I thought this was easy, but for some reason it won''t work... > Here is what I want to do: > > I am working on an API to a large database. The basic mapping is done, > but I want to implement some convenience methods that take information > for one object and does some calculations with it. The result should be > accessible as attribute. > > So in a simple example: > > We have an object that has a start and a stop attribute, i.e. columns in > the table. Now I want to know the distance, but directly via an readable > attribute. > > Class Something > > attr_accessor :distance > > def initialize > @distance = self.stop-self.start > endIf that''s an ActiveRecord objectg, not calling super (and changing the signature of initialize) is a bad thing. you could avoid that altogether with def distance @distance ||= stop - start end Fred> > end > > However, if I use my_object.distance, it returns nil... > > What am I doing wrong? > > P.S. I also tried > def initialize(distance) - but figured that only makes sense if I > specifically create a new object, not if the attribute is created from > within the class. Didn''t work either, anyway. > > Cheers, > > Marc > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Sep 17, 7:32�am, Marc Hoeppner <rails-mailing-l...@andreas-s.net> > wrote: >> So in a simple example: >> � @distance = self.stop-self.start >> �end > > If that''s an ActiveRecord objectg, not calling super (and changing the > signature of initialize) is a bad thing. you could avoid that > altogether with > > def distance > @distance ||= stop - start > end > > Fred*bangs head against wall* Thanks a lot, should have thought of that myself...sometimes it''s right there in front of you ;) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---