Skip to content

Instantly share code, notes, and snippets.

@peczenyj
Last active December 11, 2015 10:49
Show Gist options
  • Select an option

  • Save peczenyj/4589853 to your computer and use it in GitHub Desktop.

Select an option

Save peczenyj/4589853 to your computer and use it in GitHub Desktop.
I want to do the same with ruby but it is impossible. only false and nil can be considered "false" values. it is hard coded in the source of the ruby.
package BooleanClass;
use Moose;
has 'bool_value' => (
is => 'ro',
isa => 'Bool'
);
use overload
'bool' => \&_to_bool;
sub _to_bool {
shift->bool_value;
};
package main;
use Test::More tests => 2;
subtest "true values" => sub {
plan tests => 2;
my $true = BooleanClass->new( bool_value => 1 );
ok($true, "should be true");
is($true->bool_value, 1, "should return 1");
};
subtest "false values" => sub {
plan tests => 2;
my $false = BooleanClass->new( bool_value => 0 );
ok(!$false, "should be false");
is($false->bool_value, 0, "should return 0");
};
__END__
1..2
1..2
ok 1 - should be true
ok 2 - should return 1
ok 1 - true values
1..2
ok 1 - should be false
ok 2 - should return 0
ok 2 - false values
class BooleanClass:
def __init__(self, value):
self.value = value
def __nonzero__(self):
return self.value
t = BooleanClass(True)
f = BooleanClass(False)
assert t, "should be true"
assert not f, "should be false"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment