Kirbybase Hacks NilClass

  • Thread starter James Edward Gray II
  • Start date
D

Daniel Berger

Logan said:
Ooops, before someone hits me, I made a mistake in the above NULL or
FALSE is NULL not FALSE.
--Apple-Mail-20-8862340--

If only we had a Boolean class we could subclass and/or override.

Oops, that was proposed and shot down.

Dan
 
J

James Edward Gray II

select { |r| r.speed > 300 or r.range < 900 or r.service_date <
Date.today rescue false }

I realized, after I sent that, that my solution is *not* equivalent.
Guess that's a good argument for your way of doing things Jamey. ;)

James Edward Gray II
 
J

Jamey Cribbs

Trans said:
# :title: NullClass
#
# NullClass is essentially NilClass but it differs in one
# important way. When a method is called against it that it
# deoesn't have, it will simply return null value rather then
# raise an error.

class NullClass #< NilClass
class << self
def new
@null ||= NullClass.allocate
end
end
def inspect ; 'null' ; end
def nil? ; true ; end
def null? ; true ; end
def [](key); nil; end
def method_missing(sym, *args)
return nil if sym.to_s[-1,1] == '?'
self
end
end

module Kernel
def null
NullClass.new
end
end

class Object
def null?
false
end
end
Thanks, Trans! I really like this. It makes sense to just create one
null object, like nil does.

Jamey
 
J

Jamey Cribbs

James said:
I realized, after I sent that, that my solution is *not* equivalent.
Guess that's a good argument for your way of doing things Jamey. ;)

Well, I just wanted to thank you for starting this whole discussion. I
*really* like getting feedback on KirbyBase and your post has resulted
in some great ideas from people concerning this nil issue.

Bottom line is that I'm going to work on finding a way to satisfy your
request to not override NilClass#method_missing while keeping the
simplicity of KirbyBase's query expressions.

Thanks again for bringing this up.

Jamey
 
E

E. Saynatkari

Jamey said:
Well, I just wanted to thank you for starting this whole discussion. I
*really* like getting feedback on KirbyBase and your post has resulted
in some great ideas from people concerning this nil issue.

Bottom line is that I'm going to work on finding a way to satisfy your
request to not override NilClass#method_missing while keeping the
simplicity of KirbyBase's query expressions.

I may be jumping in the deep dark waters of overengineering,
but how about just proxying the call? Instead of:

r.speed # => 3

You would have

r.speed # <#KBDBField:0x...>
r.speed.value # 3
r.speed + 1 # 4

The KBDBField (it is just fun to say fast) proxies all of
its requests to the value object unless said object is nil.

Alternatively, NULL = NullClass.new is not a terrible idea :)
Thanks again for bringing this up.

Jamey


E
 
C

Chris Anderson

Trans said:
class NullClass #< NilClass

Thanks Trans! I wrote a little module from this called HungryNil, that
you can use to selectively turn nill values into nulls, in a method
chain.

eg:

nil.n #=> null

2.358.n #=> 5

nil.round #=> method missing error

nil.n.round #=> null

2.358.n.round #=> 2
 

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