# How to improve this kind of API?

**URL:** <https://www.ruby-forum.com/t/how-to-improve-this-kind-of-api/65118>\
**Category:** Ruby\
**Created:** [February 9, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/how-to-improve-this-kind-of-api/65118 "2006-02-09T20:43:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lyle\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@Lyle\_J](https://www.ruby-forum.com/u/Lyle_J)\
**Post date:** [February 9, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/how-to-improve-this-kind-of-api/65118/1 "2006-02-09T20:43:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lyle\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@Lyle\_J](https://www.ruby-forum.com/u/Lyle_J)\
**Post date:** [February 9, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/how-to-improve-this-kind-of-api/65118/2 "2006-02-09T21:26:24Z")

</div>

DÅ?a Å tvrtok 09 FebruÃ¡r 2006 20:42 Lyle J. napÃ­sal:

> splitter2.expanded = TOP\_LEFT | TOP\_RIGHT
> 
> 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 V.

---

<div class="post-metadata">

**Author:** ![Lyle\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@Lyle\_J](https://www.ruby-forum.com/u/Lyle_J)\
**Post date:** [February 9, 2006, 9:33pm UTC](https://www.ruby-forum.com/t/how-to-improve-this-kind-of-api/65118/3 "2006-02-09T21:33:24Z")

</div>

David V. wrote:

> style flag fields and doing bitwise fandango all the time. Keep up the food  
> work.

As much as we all love FXRuby, I sure hope Lyle has another source of  
food. 😉

Seriously, it’s great to hear that the FXRuby interface will be getting  
more ruby-like. I like Ara’s suggestions for easy configuration from  
YAML.load-ed hashes.

---

<div class="post-metadata">

**Author:** ![Lyle\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@Lyle\_J](https://www.ruby-forum.com/u/Lyle_J)\
**Post date:** [February 9, 2006, 10:11pm UTC](https://www.ruby-forum.com/t/how-to-improve-this-kind-of-api/65118/4 "2006-02-09T22:11:16Z")

</div>

On 2/9/06, Simon Kröger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, i haven’t used FXRuby very much, so i might miss the problem with  
> the current interface, but from what i see below i think this new  
> version doesn’t justify breaking old code.

OK.

> > For example, the 4-way  
> > splitter window can be configured to expand only its top-left pane:
> > 
> > splitter1.expanded = TOP\_LEFT
> 
> Seems perfectly fine. Ok :top\_left if more ruby’ish but also more  
> dangerous.

Why do you consider it more dangerous to use a symbol in place of a  
symbolic constant? Is it that Ruby would “notice” that a constant’s  
name is erroneous (e.g. misspelling TOP\_LEFT as TOPLEFT), but wouldn’t  
notice if you made the same mistake for a symbol or string (e.g.  
misspelling “top\_left” as “topleft”)?

> The following isn’t meant to be ‘the soultion’ but perhaps food for  
> thoughts:

Thanks. I need to ponder this…

---

<div class="post-metadata">

**Author:** ![Lyle\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@Lyle\_J](https://www.ruby-forum.com/u/Lyle_J)\
**Post date:** [February 9, 2006, 9:43pm UTC](https://www.ruby-forum.com/t/how-to-improve-this-kind-of-api/65118/5 "2006-02-09T21:43:08Z")

</div>

Lyle J. wrote:

> 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.

Well, i haven’t used FXRuby very much, so i might miss the problem with  
the current interface, but from what i see below i think this new  
version doesn’t justify breaking old code.

> For example, the 4-way  
> splitter window can be configured to expand only its top-left pane:
> 
> splitter1.expanded = TOP\_LEFT

Seems perfectly fine. Ok :top\_left if more ruby’ish but also more  
dangerous.

> or to expand both the top left and top right panes:
> 
> splitter2.expanded = TOP\_LEFT | TOP\_RIGHT

Also fine. Perhaps define a TOP constant for those who don’t like binary  
operations.

> And to check whether, say, the BOTTOM\_LEFT pane is expanded:
> 
> (splitter3.expanded & BOTTOM\_LEFT) != 0

Ok, agreed, this isn’t very nice.

> 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)

Well, i don’t like using 2 parameters when it is only one logically.

> and then having checks like:
> 
> splitter3.expanded? :bottom\_left

I don’t like that either.

> 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)

Oh, and i realy dislike this.

> 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?

## The following isn’t meant to be ‘the soultion’ but perhaps food for thoughts:

require ‘set’

class Symbol  
def method\_missing meth, \*param  
set = Set[self]  
set.send meth, param.to\_a if set.respond\_to?(meth)  
end  
end

class Splitter  
attr\_accessor :expanded  
end

TOP\_LEFT = :top\_left  
TOP\_RIGHT = :top\_right  
BOTTOM\_LEFT = :bottom\_right  
BOTTOM\_RIGHT = :bottom\_right

splitter = Splitter.new

splitter.expanded = TOP\_LEFT  
p splitter.expanded  
#=\> :top\_left

splitter.expanded = TOP\_LEFT | TOP\_RIGHT  
p splitter.expanded  
#=\>\<Set: {:top\_right, :top\_left}\>

p splitter.expanded.include?(BOTTOM\_LEFT)  
#=\> false

p splitter.expanded.include?(TOP\_LEFT)  
#=\> true

splitter.expanded = :bottom\_left | :top\_left  
p splitter.expanded  
#=\> #\<Set: {:bottom\_left, :top\_left}\>

## splitter.expanded.delete(:top\_left) p splitter.expanded #=\> #\<Set: {:bottom\_left}\>

cheers

Simon

---

<div class="post-metadata">

**Author:** ![Lyle\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@Lyle\_J](https://www.ruby-forum.com/u/Lyle_J)\
**Post date:** [February 9, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/how-to-improve-this-kind-of-api/65118/6 "2006-02-09T23:03:01Z")

</div>

DÅ?a Å tvrtok 09 FebruÃ¡r 2006 21:33 Joel VanderWerf napÃ­sal:

> David V. wrote:
> 
> > style flag fields and doing bitwise fandango all the time. Keep up the  
> > food work.
> 
> As much as we all love FXRuby, I sure hope Lyle has another source of  
> food. 😉

I cnt saem to tyep tpday…
