From: David Vallner Date: 2006-02-10T05:26:16+09:00 Subject: Re: How to improve this kind of API? Dňa Štvrtok 09 Február 2006 20:42 Lyle Johnson napísal: > I'm looking to make an API change for some methods in FXRuby and would > appreciate some suggestions about how to improve them. The pattern shows up > in several places in the code, but it always boils down to some widget > attribute that can have a combination of bit-flags. For example, the 4-way > splitter window can be configured to expand only its top-left pane: > > splitter1.expanded = TOP_LEFT > > or to expand both the top left and top right panes: > > splitter2.expanded = TOP_LEFT | TOP_RIGHT > > And to check whether, say, the BOTTOM_LEFT pane is expanded: > > (splitter3.expanded & BOTTOM_LEFT) != 0 > > Now, what I'm thinking of doing is replacing the symbolic constants with > Ruby symbols, and doing something like this instead: > > splitter1.expand(:top_left) > splitter2.expand(:top_left, :top_right) > > and then having checks like: > > splitter3.expanded? :bottom_left > > I think that in order to "turn off" one of those bits, I'm probably also > going to need to add something like: > > splitter4.unexpand(:bottom_right) > > Now, the question is, is there some existing pattern in Ruby that this > reminds you of? In other words, what is the Ruby Way to handle this kind of > setting? > > Thanks in advance for your suggestions! > > Lyle Looks fine to me. Personally I wish more GUI toolkits provided functions/methods like set_flag() and clear_flag() (or the even rarer explicit getters and setters for all applicable flags) that take flags that are to be changed instead having to futz around with unintuitive catch-all style flag fields and doing bitwise fandango all the time. Keep up the food work. David Vallner