Instantiating a subclass of NilClass

I've subclasses NilClass, but don't know how to instantiate it. Any
ideas?

  class NullClass < NilClass
    def inspect ; 'null' ; end
    def method_missing(*args) ; self ; end
  end

  module Kernel
    def null
      NullClass.allocate #?
    end
  end

T.

Hm... I guess NilClass in Java lingo would be a final class, i.e., no inheritance allowed.

Btw, why do you want to do that? I mean, you can create a class with nil-like behavior (apart from the boolean behavior I guess) without inheriting NilClass, can't you?

require 'singleton'

  class NullClass
    include Singleton

    def inspect ; 'null' ; end
    def nil?() true end
    def method_missing(*args) ; self ; end
  end

  module Kernel
    def null
      NullClass.instance
    end
  end

Kind regards

    robert

···

Trans <transfire@gmail.com> wrote:

I've subclasses NilClass, but don't know how to instantiate it. Any
ideas?

class NullClass < NilClass
   def inspect ; 'null' ; end
   def method_missing(*args) ; self ; end
end

module Kernel
   def null
     NullClass.allocate #?
   end
end

T.

Hi,

At Thu, 22 Sep 2005 23:06:39 +0900,
Trans wrote in [ruby-talk:157101]:

I've subclasses NilClass, but don't know how to instantiate it. Any
ideas?

Impossible in current implementation.

···

--
Nobu Nakada

Robert Klemme wrote:

Hm... I guess NilClass in Java lingo would be a final class, i.e., no
inheritance allowed.

Btw, why do you want to do that? I mean, you can create a class with
nil-like behavior (apart from the boolean behavior I guess) without
inheriting NilClass, can't you?

Yea, but the boolean behavior is important. I'm using it in a
OpenStruct like class, so that:

  o = OpenStructLike.new
  o.foo #=> null
  o.foo.bar #=> null

As it is:

  o.foo #=> nil
  o.foo.bar #=> ERROR

require 'singleton'

  class NullClass
    include Singleton

    def inspect ; 'null' ; end
    def nil?() true end
    def method_missing(*args) ; self ; end
  end

  module Kernel
    def null
      NullClass.instance
    end
  end

Thanks, unfortunately w/o the boolean behavior it won't work
transparently.

T.

:frowning: By any change is there another way to tap into the boolean behvior?
I oucld take robert's suggestion if I could get it to act like
nil/false.

Thanks,
T.

···

nobu.nokada@softhome.net wrote:

Hi,

At Thu, 22 Sep 2005 23:06:39 +0900,
Trans wrote in [ruby-talk:157101]:
> I've subclasses NilClass, but don't know how to instantiate it. Any
> ideas?

Impossible in current implementation.

Robert Klemme wrote:

Hm... I guess NilClass in Java lingo would be a final class, i.e.,
no inheritance allowed.

Btw, why do you want to do that? I mean, you can create a class with
nil-like behavior (apart from the boolean behavior I guess) without
inheriting NilClass, can't you?

Yea, but the boolean behavior is important. I'm using it in a
OpenStruct like class, so that:

o = OpenStructLike.new
o.foo #=> null
o.foo.bar #=> null

As it is:

o.foo #=> nil
o.foo.bar #=> ERROR

<snip/>

Thanks, unfortunately w/o the boolean behavior it won't work
transparently.

It's still not clear to me why you need that. Why can't you simply use nil? If we know the whole story we might be able to come up with a different solution. I mean, why do you need to be able to invoke methods on uninitialized members? I can remember that we had a similar discussion a while ago and your implementation of method_missing might lead to undetected errors. So I personally would not find this desirable. What makes you think differently here?

Kind regards

    robert

···

Trans <transfire@gmail.com> wrote:

Uh, isn't there a NilComparable object in the Nano/Mega stuff?

j.

···

On 9/22/05, Trans <transfire@gmail.com> wrote:

nobu.nokada@softhome.net wrote:
> Hi,
>
> At Thu, 22 Sep 2005 23:06:39 +0900,
> Trans wrote in [ruby-talk:157101]:
> > I've subclasses NilClass, but don't know how to instantiate it. Any
> > ideas?
>
> Impossible in current implementation.

:frowning: By any change is there another way to tap into the boolean behvior?
I oucld take robert's suggestion if I could get it to act like
nil/false.

Thanks,
T.

--
"http://ruby-lang.org -- do you ruby?"

Jeff Wood

Trans schrieb:

Thanks, unfortunately w/o the boolean behavior it won't work
transparently.

I thought you'd use the null-object pattern to avoid the nil? tests. You just chain messages without paying attention to null-ness.

Why do you need the boolean behavior?

Regards,
Pit

Hi,

At Fri, 23 Sep 2005 00:31:39 +0900,
Trans wrote in [ruby-talk:157124]:

> > I've subclasses NilClass, but don't know how to instantiate it. Any
> > ideas?
>
> Impossible in current implementation.

:frowning: By any change is there another way to tap into the boolean behvior?
I oucld take robert's suggestion if I could get it to act like
nil/false.

In Ruby, truth-ness is determined by the object itself, not by
its class.

···

--
Nobu Nakada

Why do you need the boolean behavior?

In particular, I use the boolean behavior to redefine an element of the
OpenStruct-like object under certain circumstances.

  unless o.m # could be null
    o.m = foo

Right now I'm using 'if o.m.nil?' but it's not nice. It means one must
be aware of ths possibiliy of a null rather than nil. And what if false
plays a role? I don't want the end user to have to worry about it. It
should be just like nil except not error when method missing.

T.

Jeff Wood wrote:

Uh, isn't there a NilComparable object in the Nano/Mega stuff?

j.

Not the same thing though.

#:title: NilComparable

···

#
# NilComparable does two things. First it makes nil comparable, such
that
# all things (except itself) are greater than it.
#
# Secondly it provides a module called NilComparable to include into
any
# other class to allow it to compare itself to NilClass as the greater
# of the two.

T.

In Ruby, truth-ness is determined by the object itself, not by
its class.

How does that work in an 'if' statement?

  if foo

How does 'foo' tell 'if' it's not truth-ful?

I'm not sure I understand the distinction you make. Isn't an object's
behavior determined by it's class?

T.

Whats the problem with just doing

def nil.method_missing(*args)
        self
end

I think this might be an issue of having your cake and eating it too.

···

On Sep 22, 2005, at 1:01 PM, Trans wrote:

Why do you need the boolean behavior?

In particular, I use the boolean behavior to redefine an element of the
OpenStruct-like object under certain circumstances.

  unless o.m # could be null
    o.m = foo

Right now I'm using 'if o.m.nil?' but it's not nice. It means one must
be aware of ths possibiliy of a null rather than nil. And what if false
plays a role? I don't want the end user to have to worry about it. It
should be just like nil except not error when method missing.

T.

Hi,

At Fri, 23 Sep 2005 08:51:39 +0900,
Trans wrote in [ruby-talk:157187]:

> In Ruby, truth-ness is determined by the object itself, not by
> its class.

How does that work in an 'if' statement?

  if foo

How does 'foo' tell 'if' it's not truth-ful?

not (nil.equals?(foo) or false.equals?(foo))

I'm not sure I understand the distinction you make. Isn't an object's
behavior determined by it's class?

No, language feature.

···

--
Nobu Nakada

Trans wrote:

  if foo

How does 'foo' tell 'if' it's not truth-ful?

It uses the RTEST() macro which currently checks if the object is either nil or false in a very fast way.

There has been requests for making RTEST() call a method on objects if it has been defined to get the truth state -- I'm one of the people who would like to see it and suggested using Object#to_bool -- however, so far the feature has been too obscure to cause a performance decrease. (Some time ago matz said that it could slow down Ruby quite a bit.)

I guess the best way of getting this into Ruby itself is to write a patch, optimize the heck out of it and send it to the friendly folks at from the ruby-core mailing list -- then fix the thousand bugs that Nobu will find in your code and resubmit. :wink:

Logan Capaldo wrote:

>> Why do you need the boolean behavior?
>>
>
> In particular, I use the boolean behavior to redefine an element of
> the
> OpenStruct-like object under certain circumstances.
>
> unless o.m # could be null
> o.m = foo
>
> Right now I'm using 'if o.m.nil?' but it's not nice. It means one must
> be aware of ths possibiliy of a null rather than nil. And what if
> false
> plays a role? I don't want the end user to have to worry about it. It
> should be just like nil except not error when method missing.
>
> T.
>
>
>

Whats the problem with just doing

def nil.method_missing(*args)
        self
end

I think this might be an issue of having your cake and eating it too.

I eat lots of cake :wink:

That can be done, but WATCH OUT! Other programs might depend on the
current behavior. And at the very least we frequently depend on nil
choking on # and other methods to tell us we have a bug in out
program. So I think its too risky. But correct me if I've over stated
the issue.

Thanks,
T.

···

On Sep 22, 2005, at 1:01 PM, Trans wrote: