Instantiating a subclass of NilClass.

T

Trans

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

Robert Klemme

Trans said:
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
 
N

nobu.nokada

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

Trans

Robert said:
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.
 
T

Trans

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.

Thanks,
T.
 
R

Robert Klemme

Trans said:
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

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
 
J

Jeff Wood

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

j.

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.

Thanks,
T.
 
P

Pit Capitain

Trans said:
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
 
T

Trans

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

Trans

Jeff said:
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.
 
L

Logan Capaldo

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

Trans

Logan said:
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.

Thanks,
T.
 
N

nobu.nokada

Hi,

At Fri, 23 Sep 2005 00:31:39 +0900,
Trans wrote in [ruby-talk:157124]:
:( 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.
 
T

Trans

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

nobu.nokada

Hi,

At Fri, 23 Sep 2005 08:51:39 +0900,
Trans wrote in [ruby-talk:157187]:
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.
 
F

Florian Groß

Trans said:
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. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top