Closed paddor closed 6 years ago
This seems to work just fine if I use virtual :use_bar, initial_value: true
instead of a default_parameter
. If I just used the library in the wrong way, you can close this issue. Thank you.
virtual :use_bar, initial_value: true
may or may not work.
BinData uses nil
values for signalling and as nil
and false
are both falsey, there may be some unexpected interaction. I'll have to examine the code to be sure.
I recommend you use ints instead.
class Foo < BinData::Record
default_parameter use_bar: 1
uint8 :foo
uint8 :bar, onlyif: ->{ use_bar == 1 }
end
Foo.read("\x00\x00", use_bar: 1)
Foo.read("\x00\x00", use_bar: 0)
Yeah, for now I'm using ints. It's just not very Ruby-like to use an int instead of true
/false
. If you think it's possible to adapt BinData, that would be awesome. Otherwise, no big issue. Thanks anyway!
Implemented in 2.4.2.
Your original example will now work as expected.
Thanks a lot! That's awesome! :smile:
I have a default parameter that is
true
by default. I want to be able to set it tofalse
on specific cases. Unfortunately, that does not work.It seems that when setting the default values as fallback, what happens is something like:
instead of:
Here's a demo:
BinData 2.4.1 on Ruby 2.5.0 prints:
Note that in the 3rd test case,
use_bar
should have beenfalse
instead oftrue
.