Forum: Ruby Re: Object#to_b

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1c1e3bdfe006a22214102fcd6434a012?d=identicon&s=25 daniels (Guest)
on 2005-11-17 01:50
(Received via mailing list)
The actual need for an Object#to_b is that there's no way to make your
own custom object that will behave in a false/nil manner.

I can't think of a usecase where I'd want to put extra information into
a nil/false object instead of using exceptions, but maybe one of those
people that keep asking about subclassing FalseClass and NilClass can
explain their use-cases.

Unless somebody can come up with a decent use-case, I'd say that we just
change the if evaluations to also allow subclasses of FalseClass and
NilClass (which will all be just singletons as they don't have new). Or
change if to look at nil? to determine whether it's true or not.



> > method Object#to_b that is called by `if' and `unless'. It
> This would break a lot of my code, personally. I used to use
>   unless var.nil? or var.foo.nil?
>   if conn
>
> which implies you're checking to see if conn is really there.
>
> -austin
> --
> Austin Ziegler * halostatue@gmail.com
>                * Alternate: austin@halostatue.ca
>
>
>
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 jeremy (Guest)
on 2005-11-17 02:12
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 16, 2005, at 4:48 PM, Daniel Sheppard wrote:
> just
> change the if evaluations to also allow subclasses of FalseClass and
> NilClass (which will all be just singletons as they don't have
> new). Or
> change if to look at nil? to determine whether it's true or not.

In Active Record:

   class Foo < ActiveRecord::Base
     has_one :bar
   end

I want a simple test for presence of bar, so return nil from
Foo#bar if there is no associated object:

   if foo.bar
     # ...
   end

I also want to treat Foo#bar as a reflection of the association
so I can have meta-methods for querying the column type, creating
objects, performing scoped queries, etc:

   puts foo.bar.column_type
   foo.bar.create(:name => 'first bar')

This usage feels very expressive and is possible for one-to-many
and many-to-many associations because acting like an Array is not
a problem.  But for one-to-one associations, we must act like nil,
which is impossible due to RTEST doing direct object comparison.

to_b is one way to do it, but I think it is too disruptive.
I would prefer to define #nil? and have RTEST call it.

Best,
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDe9iQAQHALep9HFYRArk3AJ4/tPb3QTnptm5LGFL2+s9MJlO5BgCeOGZH
DHvBGN0h+pD/ReEtk9kcjLk=
=TB0L
-----END PGP SIGNATURE-----
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 matz (Guest)
on 2005-11-17 02:12
(Received via mailing list)
Hi,

In message "Re: Object#to_b"
    on Thu, 17 Nov 2005 09:48:18 +0900, "Daniel Sheppard"
<daniels@pronto.com.au> writes:

|The actual need for an Object#to_b is that there's no way to make your
|own custom object that will behave in a false/nil manner.

Yes.  And I believe it's good.  There's no possibility for us to
confuse whether something is false.  false is false, nil is false,
that's all.

In general, such attempt to define new false values is not worth the
performance cost we all have to pay.

							matz.
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-11-17 02:24
(Received via mailing list)
On 11/16/05, Daniel Sheppard <daniels@pronto.com.au> wrote:
> The actual need for an Object#to_b is that there's no way to make your
> own custom object that will behave in a false/nil manner.

I don't know that this is a good or useful thing. I think that it would
decrease the overall clarity of Ruby code.

> I can't think of a usecase where I'd want to put extra information
> into a nil/false object instead of using exceptions, but maybe one of
> those people that keep asking about subclassing FalseClass and
> NilClass can explain their use-cases.

And I think that if they do, they'll be directed to different possible
solutions.

> Unless somebody can come up with a decent use-case, I'd say that we
> just change the if evaluations to also allow subclasses of FalseClass
> and NilClass (which will all be just singletons as they don't have
> new). Or change if to look at nil? to determine whether it's true or
> not.

Hm. No, I don't think that either is good. I would suggest that neither
FalseClass nor NilClass should meaningfully have subclasses and that if
should *not* look at #nil?. I don't think that there is a good case to
be found
for this at all.

-austin
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 jeremy (Guest)
on 2005-11-17 02:27
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 16, 2005, at 5:11 PM, Yukihiro Matsumoto wrote:
> that's all.
I agree; I have always liked this behavior.


> In general, such attempt to define new false values is not worth the
> performance cost we all have to pay.

I would not like new false values, but I would like to be able to proxy
the existing false values.  Especially nil.

What is the cost?

Best,
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDe9vrAQHALep9HFYRAje9AKDR7d9/ej5+JwGYxDfBiH7zxBlDZwCgzUg0
Z61NdDino/WTrg4sz1CrMEY=
=apF5
-----END PGP SIGNATURE-----
Dd76a12d66f843de5c5f8782668e7127?d=identicon&s=25 mfp (Guest)
on 2005-11-17 03:03
(Received via mailing list)
On Thu, Nov 17, 2005 at 10:25:03AM +0900, Jeremy Kemper wrote:
> On Nov 16, 2005, at 5:11 PM, Yukihiro Matsumoto wrote:
> >In general, such attempt to define new false values is not worth the
> >performance cost we all have to pay.
>
> I would not like new false values, but I would like to be able to proxy
> the existing false values.  Especially nil.
>
> What is the cost?

A full method dispatch instead of an inlined test

  #define RTEST(v) (((VALUE)(v) & ~Qnil) != 0)
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-11-17 15:50
(Received via mailing list)
Quoting Jeremy Kemper <jeremy@bitsweat.net>:

> I would not like new false values, but I would like to be able to
> proxy the existing false values.  Especially nil.

Ah, fudge.  I hadn't thought of that.  That is one place where
lazy.rb falls down.

promise { false } will always be true :/

(unless you use an explicitly force()'d value.)

> What is the cost [of allowing NilClass and FalseClass subclasses]?

I don't see any way that it could be as efficient as the current
setup, where nil and false are immediate values and are checked
with a simple bitmask.

-mental
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-11-17 16:14
(Received via mailing list)
> I don't see any way that it could be as efficient as the current
> setup, where nil and false are immediate values and are checked
> with a simple bitmask.

Could they get special object_id's that are easy to check?

T.
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-11-17 16:41
(Received via mailing list)
Quoting Trans <transfire@gmail.com>:

> > I don't see any way that it could be as efficient as the
> current
> > setup, where nil and false are immediate values and are checked
> > with a simple bitmask.
>
> Could they get special object_id's that are easy to check?

Like...?

Currently, for 32-bit systems:

            false: 00000000000000000000000000000000
             true: 00000000000000000000000000000010
              nil: 00000000000000000000000000000100
            undef: 00000000000000000000000000000110
           Symbol: XXXXXXXXXXXXXXXXXXXXXXXX00001110
 Object reference: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX0
           Fixnum: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1

That means we get 24 bits worth of symbols, 31 (signed) bits of
fixnums, and 31-bit pointers to objects (so objects must be aligned
on two-byte boundaries).

-mental
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 jeremy (Guest)
on 2005-11-17 21:05
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 17, 2005, at 6:49 AM, mental@rydia.net wrote:
> (unless you use an explicitly force()'d value.)
>
>> What is the cost [of allowing NilClass and FalseClass subclasses]?
>
> I don't see any way that it could be as efficient as the current
> setup, where nil and false are immediate values and are checked
> with a simple bitmask.

Right: I had to dig as far as RTEST when I tried to act like nil
to understand why it did not work.  I was happy to see that it's
very fast but was unhappy to discover the "line in the sand" between
dynamic, expressive Ruby and its concern for raw performance.

There is a Moore's law for one but not the other.

Has anyone measured the cost of doing boolean tests the Ruby way?
Clearly it will be much slower as a microbenchmark, but perhaps the
overall cost for most Ruby programs is low enough to tip the balance.

And if not now, perhaps in one year.

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDfOJJAQHALep9HFYRAjZyAJ42wnfNTnsiqM0E4TFNzaoVITT7AwCffZp0
ONJn0imCyJI3g0CR7l+A3PA=
=TSa9
-----END PGP SIGNATURE-----
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 gwtmp01 (Guest)
on 2005-11-17 22:29
(Received via mailing list)
On Nov 17, 2005, at 3:04 PM, Jeremy Kemper wrote:
> Right: I had to dig as far as RTEST when I tried to act like nil
> to understand why it did not work.  I was happy to see that it's
> very fast but was unhappy to discover the "line in the sand" between
> dynamic, expressive Ruby and its concern for raw performance.

I think one of the interesting/useful features of Ruby is its
handling of boolean values.  In thinking about the original post,
I came across a nice discussion of nil, null, and false in object
oriented languages, in particular Smalltalk and Objective-C.

http://www.smalltalkchronicles.net/edition2-1/null...

I really like the null object pattern but I'm not sure I'm ready
to fully embrace it with a generalized null object and even then I'm
not sure how I think that should fit with nil/false.

My hunch is that either you have a generalized null object that is
considered false or you don't have it.  The idea of having user-defined
false objects just seems to be a opening a Pandora's box of issues.

Gary Wright
This topic is locked and can not be replied to.