I''m trying to set attributes of widgets in subclasses like the following. class PersonTable < FXTable def initialize(owner) options = TABLE_COL_SIZEABLE super(owner, nil, 0, options) visibleRows = 5 ... end end visibleRows doesn''t get set. However, if I do this self.visibleRows = 5 then it does. Shouldn''t it work without "self."? -- R. Mark Volkmann Object Computing, Inc.
Mark Volkmann wrote:> I''m trying to set attributes of widgets in subclasses like the following. > > class PersonTable < FXTable > def initialize(owner) > options = TABLE_COL_SIZEABLE > super(owner, nil, 0, options) > > visibleRows = 5 > ... > end > end > > visibleRows doesn''t get set. > However, if I do this > > self.visibleRows = 5 > > then it does. > > Shouldn''t it work without "self."?That''s a general ruby principle: any expression like identifier = value assigns to a local variable, even if there is a method identifier= . -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
On Mar 28, 2006, at 1:32 PM, Mark Volkmann wrote:> However, if I do this > > self.visibleRows = 5 > > then it does. > > Shouldn''t it work without "self."?Nope, and this is a Ruby thing (not particular to FXRuby, I mean). When you do: visibleRows = 5 Ruby assumes that you''re initializing a local variable. You only invoke the accessor method for the visibleRows attribute when you prepend it with self (as you''ve discovered).
On 3/28/06, Lyle Johnson <lyle at knology.net> wrote:> > On Mar 28, 2006, at 1:32 PM, Mark Volkmann wrote: > > > However, if I do this > > > > self.visibleRows = 5 > > > > then it does. > > > > Shouldn''t it work without "self."? > > Nope, and this is a Ruby thing (not particular to FXRuby, I mean). When > you do: > > visibleRows = 5 > > Ruby assumes that you''re initializing a local variable. You only invoke > the accessor method for the visibleRows attribute when you prepend it > with self (as you''ve discovered).Since visibleRows is listed as an RW "attribute" in the RDoc, I thought that implied there were methods named "visibleRows" and visibleRows=" available. That''s what I thought I was invoking with "visibleRows = 5". If those methods existed then Ruby wouldn''t have thought I wanted to initialize a local variable. Right? Since those accessor methods don''t exist, I''ll just use "@visibleRows = 5". -- R. Mark Volkmann Object Computing, Inc.
On 3/28/06, Mark Volkmann <r.mark.volkmann at gmail.com> wrote:> I''m trying to set attributes of widgets in subclasses like the following. > > class PersonTable < FXTable > def initialize(owner) > options = TABLE_COL_SIZEABLE > super(owner, nil, 0, options) > > visibleRows = 5 > ... > end > end > > visibleRows doesn''t get set. > However, if I do this > > self.visibleRows = 5 > > then it does. > > Shouldn''t it work without "self."?that''s a common problem with ruby extensions. but i don''t understand the underlying principle ;) -- henon
On 3/28/06, Mark Volkmann <r.mark.volkmann at gmail.com> wrote:> On 3/28/06, Lyle Johnson <lyle at knology.net> wrote: > > > > On Mar 28, 2006, at 1:32 PM, Mark Volkmann wrote: > > > > > However, if I do this > > > > > > self.visibleRows = 5 > > > > > > then it does. > > > > > > Shouldn''t it work without "self."? > > > > Nope, and this is a Ruby thing (not particular to FXRuby, I mean). When > > you do: > > > > visibleRows = 5 > > > > Ruby assumes that you''re initializing a local variable. You only invoke > > the accessor method for the visibleRows attribute when you prepend it > > with self (as you''ve discovered). > > Since visibleRows is listed as an RW "attribute" in the RDoc, I > thought that implied there were methods named "visibleRows" and > visibleRows=" available. That''s what I thought I was invoking with > "visibleRows = 5". If those methods existed then Ruby wouldn''t have > thought I wanted to initialize a local variable. Right? > > Since those accessor methods don''t exist, I''ll just use "@visibleRows = 5".Eeek! This doesn''t work. I''m so confused. Is visibleRows an instance variable of FXTable? If so, why can''t I access it directly from a subclass with "@visibleRows"? Does it have accessor methods? If so, why can''t I invoke them directly from a subclass without using "self."? Sorry to ask basic Ruby questions. I thought I understood this, but apparently not. -- R. Mark Volkmann Object Computing, Inc.
On 3/28/06, Meinrad Recheis <meinrad.recheis at gmail.com> wrote:> On 3/28/06, Mark Volkmann <r.mark.volkmann at gmail.com> wrote: > > I''m trying to set attributes of widgets in subclasses like the following. > > > > class PersonTable < FXTable > > def initialize(owner) > > options = TABLE_COL_SIZEABLE > > super(owner, nil, 0, options) > > > > visibleRows = 5 > > ... > > end > > end > > > > visibleRows doesn''t get set. > > However, if I do this > > > > self.visibleRows = 5 > > > > then it does. > > > > Shouldn''t it work without "self."? > > that''s a common problem with ruby extensions. but i don''t understand > the underlying principle ;)thanks to joel now i do :D
On Tue, 28 Mar 2006 15:08:02 -0600, "Mark Volkmann" <r.mark.volkmann at gmail.com> wrote :> Since visibleRows is listed as an RW "attribute" in the RDoc, I > thought that implied there were methods named "visibleRows" and > visibleRows=" available.There are! You just have to be careful about how you invoke those methods.> That''s what I thought I was invoking with > "visibleRows = 5". If those methods existed then Ruby wouldn''t have > thought I wanted to initialize a local variable. Right?I''m not Matz; you are arguing with the wrong person. ;)
On 3/28/06, Mark Volkmann <r.mark.volkmann at gmail.com> wrote:> On 3/28/06, Lyle Johnson <lyle at knology.net> wrote: > > > > On Mar 28, 2006, at 1:32 PM, Mark Volkmann wrote: > > > > > However, if I do this > > > > > > self.visibleRows = 5 > > > > > > then it does. > > > > > > Shouldn''t it work without "self."? > > > > Nope, and this is a Ruby thing (not particular to FXRuby, I mean). When > > you do: > > > > visibleRows = 5 > > > > Ruby assumes that you''re initializing a local variable. You only invoke > > the accessor method for the visibleRows attribute when you prepend it > > with self (as you''ve discovered). > > Since visibleRows is listed as an RW "attribute" in the RDoc, I > thought that implied there were methods named "visibleRows" and > visibleRows=" available. That''s what I thought I was invoking with > "visibleRows = 5". If those methods existed then Ruby wouldn''t have > thought I wanted to initialize a local variable. Right? > > Since those accessor methods don''t exist, I''ll just use "@visibleRows = 5".that won''t work ;) all attributes are accessible via methods only. there are no instance variables (with some exceptions) because the attributes have to be passed to the underlying C++ library. -- henon
On Tue, 28 Mar 2006 15:14:35 -0600, "Mark Volkmann" <r.mark.volkmann at gmail.com> wrote :> Eeek! This doesn''t work. I''m so confused. > > Is visibleRows an instance variable of FXTable? > If so, why can''t I access it directly from a subclass with "@visibleRows"?No, @visibleRows is not a real instance variable for FXTable. That value is buried way down deep in the C++ object.> Does it have accessor methods?Yes, but they are more along the lines of what Dave and Andy called "virtual attributes". See the "Classes, Objects and Variables" chapter of Programming Ruby (http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html), and look at the section called "Virtual Attributes".> If so, why can''t I invoke them directly from a subclass > without using "self."?As I''ve noted in other replies, this is just how Ruby works. There is some discussion of this in Programming Ruby as well; see the sidebar titled "Using Accessors Within a Class" in the "Expressions" chapter (http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html).
On 3/28/06, Meinrad Recheis <meinrad.recheis at gmail.com> wrote:> On 3/28/06, Mark Volkmann <r.mark.volkmann at gmail.com> wrote: > > I''m trying to set attributes of widgets in subclasses like the following. > > > > class PersonTable < FXTable > > def initialize(owner) > > options = TABLE_COL_SIZEABLE > > super(owner, nil, 0, options) > > > > visibleRows = 5 > > ... > > end > > end > > > > visibleRows doesn''t get set. > > However, if I do this > > > > self.visibleRows = 5 > > > > then it does. > > > > Shouldn''t it work without "self."? > > that''s a common problem with ruby extensions. but i don''t understand > the underlying principle ;)I think I do now, so I''ll try to pass on what is happening. When Ruby sees something like this foo bar it starts by checking the current object referred to by self to see if it has a method named "foo". If it does, that is used. If it doesn''t, the method search continues through singleton methods, class methods, included module methods, and up the inheritance hierachy. But what about a method named "foo="? I assumed that if I wrote foo = bar the same method search would apply. However, it does not. It will just create a local variable named foo and assign bar to it. But if I write self.foo = bar then it knows I''m invoking a method and it knows the receiver and will look for a foo= method in the current object. -- R. Mark Volkmann Object Computing, Inc.
On 3/28/06, Mark Volkmann <r.mark.volkmann at gmail.com> wrote:> On 3/28/06, Mark Volkmann <r.mark.volkmann at gmail.com> wrote: > > On 3/28/06, Lyle Johnson <lyle at knology.net> wrote: > > > > > > On Mar 28, 2006, at 1:32 PM, Mark Volkmann wrote: > > > > > > > However, if I do this > > > > > > > > self.visibleRows = 5 > > > > > > > > then it does. > > > > > > > > Shouldn''t it work without "self."? > > > > > > Nope, and this is a Ruby thing (not particular to FXRuby, I mean). When > > > you do: > > > > > > visibleRows = 5 > > > > > > Ruby assumes that you''re initializing a local variable. You only invoke > > > the accessor method for the visibleRows attribute when you prepend it > > > with self (as you''ve discovered). > > > > Since visibleRows is listed as an RW "attribute" in the RDoc, I > > thought that implied there were methods named "visibleRows" and > > visibleRows=" available. That''s what I thought I was invoking with > > "visibleRows = 5". If those methods existed then Ruby wouldn''t have > > thought I wanted to initialize a local variable. Right? > > > > Since those accessor methods don''t exist, I''ll just use "@visibleRows = 5". > > Eeek! This doesn''t work. I''m so confused. > > Is visibleRows an instance variable of FXTable? > If so, why can''t I access it directly from a subclass with "@visibleRows"?no it isn''t as i have explained in my previous mail.> > Does it have accessor methods? > If so, why can''t I invoke them directly from a subclass without using "self."?yes, the accessors exist. but as joel has explained bevore ruby assumes that you are defining local variables so you need to use self.> > Sorry to ask basic Ruby questions. I thought I understood this, but > apparently not.no problem. hope that helps, -- henon