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
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.
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?
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.
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 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.
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.
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.
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.
>> 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
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.