# Enable input and Ruby true/false semantics

**URL:** <https://www.ruby-forum.com/t/enable-input-and-ruby-true-false-semantics/208992>\
**Category:** wxRuby\
**Created:** [July 2, 2011, 1:16am UTC](https://www.ruby-forum.com/t/enable-input-and-ruby-true-false-semantics/208992 "2011-07-02T01:16:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 2, 2011, 1:16am UTC](https://www.ruby-forum.com/t/enable-input-and-ruby-true-false-semantics/208992/1 "2011-07-02T01:16:27Z")

</div>

I’m working on enabling/disabling controls, and I keep getting  
exceptions like the following:

mysource.rb:262:in `enable': Expected argument 1 of type bool, but got NilClass nil (TypeError) in SWIG method 'Enable' from mysource.rb:262 from C:/Ruby187/lib/ruby/gems/1.8/gems/wxruby-2.0.1-x86-mingw32/lib/wx/classe s/app.rb:16:in`call’  
from  
C:/Ruby187/lib/ruby/gems/1.8/gems/wxruby-2.0.1-x86-mingw32/lib/wx/classe  
s/app.rb:16:in `process_event' from C:/Ruby187/lib/ruby/gems/1.8/gems/wxruby-2.0.1-x86-mingw32/lib/wx/classe s/app.rb:16:in`on\_run’  
from  
C:/Ruby187/lib/ruby/gems/1.8/gems/wxruby-2.0.1-x86-mingw32/lib/wx/classe  
s/app.rb:16:in `main_loop' from C:/Ruby187/lib/ruby/gems/1.8/gems/wxruby-2.0.1-x86-mingw32/lib/wx/classe s/app.rb:16:in`run’  
from app.rb:188

Is there an (easy) way to help WxRuby follow Ruby’s notion of truth,  
i.e. everything but +nil+ and +false+ are true?

The current best workaround I have is

enable(… ? true : false)

but that’s clunky, and I obviously keep forgetting to use it. 🙂

Thanks!

* * *

Ryan H.  
L-3 Communications / Communication Systems West  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 4, 2011, 12:01pm UTC](https://www.ruby-forum.com/t/enable-input-and-ruby-true-false-semantics/208992/2 "2011-07-04T12:01:11Z")

</div>

Hi Ryan

[list generally - I’ve had problems receiving wxruby-users posts in the  
past month - apologies for any missed discussions]

On 02/07/2011 00:10, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m working on enabling/disabling controls, and I keep getting  
> exceptions like the following:
> 
> mysource.rb:262:in `enable’: Expected argument 1 of type bool, but got  
> NilClass nil (TypeError)  
> in SWIG method ‘Enable’  
> from mysource.rb:262  
> from

These exceptions come from SWIG’s default way of wrapping C/C++ methods  
that require a C boolean type.

> Is there an (easy) way to help WxRuby follow Ruby’s notion of truth,  
> i.e. everything but +nil+ and +false+ are true?
> 
> The current best workaround I have is
> 
> enable(… ? true : false)
> 
> but that’s clunky, and I obviously keep forgetting to use it. 🙂

Globally, it could be changed by altering the standard way that wxRuby  
SWIG translates methods that require a boolean. It is a pretty simple  
%typemap statement but obviously requires the whole SWIG+compiler  
set-up. However it’s a change I’d be open to adding for future versions,  
as I think SWIG’s approach is sometimes too class-strict for Ruby style.

For the time being, the only way would be either to add your workaround  
to a subclass of the control, so that its enable method looked like  
super(input ? true : false). Or you could do some reflection and find  
methods called ‘enable’ in all wxRuby classes at start-up and redefine  
them. But from experience (at least with Ruby 1.8) it does take a little  
time to cycle through all the 200+ classes.

best  
alex

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 6, 2011, 4:22pm UTC](https://www.ruby-forum.com/t/enable-input-and-ruby-true-false-semantics/208992/3 "2011-07-06T16:22:41Z")

</div>

Alex:

Thanks for the RubyForge tracker pointer. I’ll add this to the list.

FYI, I just squashed a related bug. I was returning +nil+ when a  
drag-drop operation failed, and it would crash my application. I just  
added the

? true : false

trick on my return value, and the crash is gone. I assume the SWIG  
solution would handle this case, too.

Thanks!

- Ryan

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 6, 2011, 11:48am UTC](https://www.ruby-forum.com/t/enable-input-and-ruby-true-false-semantics/208992/4 "2011-07-06T11:48:47Z")

</div>

> I’m already a bit over-budget; I won’t have time to set up the full  
> environment now. Is there a bug-tracking system I can enter this as an  
> enhancement request?

[http://rubyforge.org/tracker/?atid=221&group\_id=35&func=browse](http://rubyforge.org/tracker/?atid=221&group_id=35&func=browse)

cheers  
alex

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 6, 2011, 4:27pm UTC](https://www.ruby-forum.com/t/enable-input-and-ruby-true-false-semantics/208992/5 "2011-07-06T16:27:02Z")

</div>

On 05/07/11 22:18, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> FYI, I just squashed a related bug. I was returning +nil+ when a  
> drag-drop operation failed, and it would crash my application. I just  
> added the
> 
> ? true : false
> 
> trick on my return value, and the crash is gone. I assume the SWIG  
> solution would handle this case, too.

Thanks for the info. Standard SWIG covers straightforward cases (e.g.  
Ruby arguments to C++ methods). Some stuff like drag-drop where C++ code  
calls Ruby then back to C++ is more tricky and there may be unsafe  
conversions. So it would be great if you could add a couple of details  
in a bug report.

alex

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 11, 2011, 4:31pm UTC](https://www.ruby-forum.com/t/enable-input-and-ruby-true-false-semantics/208992/6 "2011-07-11T16:31:26Z")

</div>

On Tue, Jul 5, 2011 at 5:18 PM, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> trick on my return value, and the crash is gone. I assume the SWIG  
> Subject: Re: [wxruby-users] Enable input and Ruby true/false semantics
> 
> > > I’m working on enabling/disabling controls, and I keep getting  
> > > methods  
> > > but that’s clunky, and I obviously keep forgetting to use it. 🙂  
> > > I’m already a bit over-budget; I won’t have time to set up the full  
> > > little  
> > > wxruby-users mailing list  
> > > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > > [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

I realize I’m a bit late and not really solving the core issue, but: one  
of  
the common ways to approach forcing a boolean datatype, in the rails  
world  
at least, is to double up your boolean not operators: !!.  
It’s a  
little weird looking at first but gets the same results as (  
?  
true : false) with less typing.

–Stephen
