NEWBIE QUESTION: pattern with nil

B

basi

Hello,
I'm a bit tired typing:

if a == "" or a == nil
if a != "" and a !== nil

I'm sure there is a Ruby way to do this.

Thanks for the help.
basi
 
A

Ara.T.Howard

Hello,
I'm a bit tired typing:

if a == "" or a == nil
if a != "" and a !== nil

I'm sure there is a Ruby way to do this.

Thanks for the help.
basi

i generally use

harp:~ > cat a.rb
a = 'foobar'
p 42 unless a.nil? or a.empty?

a = nil
p 42 unless a.nil? or a.empty?

harp:~ > ruby a.rb
42

because the meaning is very clear. for shorter i use

harp:~ > cat a.rb
a = 'foobar'
p 42 unless a.to_s.empty?

a = nil
p 42 unless a.to_s.empty?

harp:~ > ruby a.rb
42


cheers.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
D

David A. Black

Hi --

Hello,
I'm a bit tired typing:

if a == "" or a == nil
if a != "" and a !== nil

I'm sure there is a Ruby way to do this.

You can normalize it to a string:

if a.to_s.empty?

or, for the opposite:

unless a.to_s.empty?


David
 
A

Ara.T.Howard

Hi --



You can normalize it to a string:

if a.to_s.empty?

or, for the opposite:

unless a.to_s.empty?

'normalize' is such a nicer word than 'cast' - i'll take it!

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
J

John Carter

Hello,
I'm a bit tired typing:

if a == "" or a == nil
if a != "" and a !== nil

I'm sure there is a Ruby way to do this.


class NilClass
def empty?
true
end
end

if a.empty?




John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.
 
B

BearItAll

class NilClass
def empty?
true
end
end

if a.empty?

I vote for this one because a good programmer is a lazy sod. Where ever
you would end up doing a thing more than once, write it is such a way that
you never have to type it again.
 
A

Austin Ziegler

I vote for this one because a good programmer is a lazy sod. Where ever
you would end up doing a thing more than once, write it is such a way tha= t
you never have to type it again.

Just, please, don't do it in a library that you unleash on the rest of
the world. It changes the contract for +nil+, and I prefer that it
*not* respond to #empty?, because the concept isn't *quite* accurate.
One may as well define #zero? if you're going to to that (but +nil+
isn't zero, either, just as it isn't the empty string or array).
Better is to use the foo.nil? or foo.empty? construct and be explicit,
or foo.to_s.empty? if you want to silently cast ("normalize").
Changing nil to respond to #empty? is an implicit change that also
changes its meaning.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
W

Wilson Bilkovich

Rails defines a method called "blank?" on the class Object.
Here's what it looks like:

# "", " ", nil, and 0 are all blank
def blank?
if respond_to?:)empty?) && respond_to?:)strip)
strip.empty?
elsif respond_to? :empty?
empty?
elsif respond_to? :zero?
zero?
else
!self
end
end
 
B

basi

David said:
Hi --



You can normalize it to a string:

if a.to_s.empty?

or, for the opposite:

unless a.to_s.empty?


David

Thanks all for the suggestions. This solution appeals to me because I
don't have to define anything new. I wonder if, in general, this is a
good criteria for evaluating solutions.

basi
 
A

Aredridel

Thanks all for the suggestions. This solution appeals to me because I
don't have to define anything new. I wonder if, in general, this is a
good criteria for evaluating solutions.

Certainly not the only criteria one should use -- but it sure feels
right. It's the same reason I'd use an array, not some ContainerOfFoo
class that I define -- if the builtins do the job, use them!
 
J

James Edward Gray II

Thanks all for the suggestions. This solution appeals to me because I
don't have to define anything new. I wonder if, in general, this is a
good criteria for evaluating solutions.

My two cents:

Ruby makes it trivially easy to alter the behavior of the core
classes. It's a powerful feature and I love it, but it's also easy
to abuse.

If you're working on a Domain Specific Language and want to alter the
core classes to fit that model, I say go for it. If you can add a
few methods to Array or String or whatever to help with the task you
are currently tackling, again I say do it.

However, I'm usually against hacks to change the behavior of the core
classes. You could break a lot of code that way, possibly in Ruby's
rich standard library. Plus, your teaching yourself to rely on non-
core techniques, which I doubt will help in the long run. Put in
terms of this thread, I'm fine with Rails' blank() method (just
adding some functionality), but leery of adding empty?() to nil
(that's not how Ruby's nil works).

Again though, that's all just my opinion. Obviously, Ruby allows
people to add empty?() to nil and some do.

James Edward Gray II
 
J

John Carter

Just, please, don't do it in a library that you unleash on the rest of
the world. It changes the contract for +nil+, and I prefer that it
*not* respond to #empty?, because the concept isn't *quite* accurate.

Well...

I did say long and loud and tediously in the RCR 303 discussion that we
there are several meanings to nil and we need to change ruby to
distinguish between them.

The original poster had a NoThing type of nil. Your fear arises from the
possibility of applying the empty? method to an Unitialized or
NotApplicable type of nil.




John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.
 
D

David A. Black

Hi --

Well...

I did say long and loud and tediously in the RCR 303 discussion that we there
are several meanings to nil and we need to change ruby to distinguish between
them.

The original poster had a NoThing type of nil. Your fear arises from the
possibility of applying the empty? method to an Unitialized or NotApplicable
type of nil.

I would say that asking whether nil is empty is like asking whether 12
is empty: it just doesn't mean anything. Even having it say "false"
suggests that "true" is possible. In general, I don't think
non-container objects should be declaring themselves "empty".


David
 
G

Gavin Kistner

Thanks all for the suggestions. This solution appeals to me because I
don't have to define anything new. I wonder if, in general, this is a
good criteria for evaluating solutions.
[snip]
However, I'm usually against hacks to change the behavior of the
core classes. You could break a lot of code that way, possibly in
Ruby's rich standard library. Plus, your teaching yourself to rely
on non-core techniques, which I doubt will help in the long run.
[snip]

This has been the biggest 'gotcha' for me; I started out with Ruby
and slowly grew my own library of really convenient (to me) additions/
changes to the core libraries. Invariably, I get to a point in my
code where I want to release it, and I'm left with three choices:

a) Require people to include my library-of-hacks (in whole, since I
don't have it broken into tiny pieces) if they want to use my sweet
new library I'm releasing. Not a nice dependency.

b) Copy/paste the salient hacks into my library file so they go along
together. Ugh, now I have a version control problem.

c) Rewrite my library not to use my own hacks, but instead strive to
use only built-in stuff.

Usually, I choose (c).


If you're doing programs just for yourself, I personally suggest you
do whatever makes your life easier. But the addiction to customizing
the language can be problematic if you want to share your code.
 
J

John Carter

I would say that asking whether nil is empty is like asking whether 12
is empty: it just doesn't mean anything. Even having it say "false"
suggests that "true" is possible. In general, I don't think
non-container objects should be declaring themselves "empty".

class Fixnum
def each
# I'm throwing this exception, not because it is required,
# but because a headache is damping my imagination
# as to what -1.each means. I think it means the something
# like collect or sum, but my head hurts too much to say.
raise "Negative number" if self < 0

(1..self).each{|i| yield 1}
end

def empty?
self == 0
end
end

12.each {|i| puts i}

1
1
1
1
1
1
1
1
1
1
1
1


What does NoThing contain?

Obvious it contains NoThing.

noThing.shift returns noThing.

Is NoThing empty?

Yes.

It's all like non-euclidean geometry. Nobody could prove Euclid's 5
postulate even though it was ugly enough to be a theorem not an axiom.

So some bright spark resolve to propose an alternative, intuitively wrong
alternative to postulate 5 and then find the contradiction.

There wasn't one.

ie. Equally valid non-euclidean geometries exist.

The current axioms of "nil" are valid and consistent.

But more useful, equally consistent axioms of "nil" can be proposed.




John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.
 
J

John Carter

c) Rewrite my library not to use my own hacks, but instead strive to use only
built-in stuff.

My whole reply was a little tease....

My Hidden Agenda was...

* Looky See, very very nice NullObject pattern solution to this problem.

* Wait for the chorus of Yums! As people see how simple and tasty it is.

* Wait for the echo of Yucks as curmudgeons like you correctly note the
downside.

* Pop up and say, "I told you so, we need to disambiguate the meanings of
nil!"

Unfortunately the poor newbie gets sorrowfully confused in the process.

Sorry mate.



John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.
 
D

David A. Black

Hi --

class Fixnum
def each
# I'm throwing this exception, not because it is required,
# but because a headache is damping my imagination
# as to what -1.each means. I think it means the something
# like collect or sum, but my head hurts too much to say.
raise "Negative number" if self < 0

(1..self).each{|i| yield 1}
end

def empty?
self == 0
end
end

12.each {|i| puts i}

Well, yes, Ruby lets you do that. I would still argue that "it just
doesn't mean anything" :) I don't think that 0 is empty.
Containers contain 0 elements are empty. 0 isn't a container.
What does NoThing contain? Obvious it contains NoThing.
noThing.shift returns noThing. Is NoThing empty? Yes.

This is a bit like "a ham sandwich is better than God" :) [1] I'd
actually say there's a difference between: it contains [Nn]o[Tt]hing,
and: it's not a container.
The current axioms of "nil" are valid and consistent. But more
useful, equally consistent axioms of "nil" can be proposed.

I look at it the other way around: not "What can nil evolve to from
what it is?" but "Is it desireable to have an object that has
no dimensional characteristics (no size, no fullness or emptiness,
etc.)?" If it's good to have that, then it will either be nil or
whatever new term is created to replace nil if nil is changed. So I'd
just as soon keep nil nil and come up with something else if there's a
demonstrated need for something else.

Anyway, in practical terms I would certainly agree with Austin that
one should not change nil in any code that one plans to share.


David

[1] I don't where it originated, but the syllogism goes: A ham
sandwich is better than nothing. Nothing is better than God. Ergo...
 
A

Austin Ziegler

I did say long and loud and tediously in the RCR 303 discussion
that we there are several meanings to nil and we need to change
ruby to distinguish between them.

I didn't go into detail there, John, but it's worth noting that
originally Codd thought that there should be several different NULL
values for relational databases. Later in life, he came to believe
that NULL values (both the single NULL value in SQL databases and
the multiple NULL values in his original relational papers) were
bad.
The original poster had a NoThing type of nil. Your fear arises
from the possibility of applying the empty? method to an
Unitialized or NotApplicable type of nil.

Your proposal of multiple types of nil makes life harder. Instead
of:

if foo.nil?
# blah
end

I would have to do:

if foo.nothing? or foo.undefined? or foo.not_applicable?
# blah
end

The problem, as others have stated, is that adding #empty? to
NilClass implies a containment. I'd have as big a problem if we
added #empty? to Fixnum so that 0 returned true and everything else
returned false. (Indeed, would that be correct? Would -3.empty? be
true or false?)

Better to do:

if foo.nil? or foo.empty?
# blah
end

The code is explicit and meaningful at that point. Multiple types of
NULL/nil values tend to confuse things far more than they help
things.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
J

John Carter

I didn't go into detail there, John, but it's worth noting that
originally Codd thought that there should be several different NULL
values for relational databases. Later in life, he came to believe
that NULL values (both the single NULL value in SQL databases and
the multiple NULL values in his original relational papers) were
bad.


No, as I remember it, his problem wasn't with NULL, but
rather the DB design.

ie. All instances where you have NotApplicable or NoThing null IN A
DB TABLE can be refactored to a better relational design that didn't
require them.

However, having factored the DB into more tables so neither variety of
NULL occurred, there is still a need for NULL. There is still a need on
occasion to construct reports for user display which are joins of various
tables. And in those "views" there may be some fields are going to be
NULL. And again, it is still necessary to disambiguate between
NotApplicable and NoThing.
Your proposal of multiple types of nil makes life harder. Instead
of:

if foo.nil?
# blah
end

I would have to do:

if foo.nothing? or foo.undefined? or foo.not_applicable?
# blah
end

How about starting with the following in the core

class Object

# This is as is already
def nil?
false
end

def uninitialized?
false
end

def not_applicable?
false
end


def nothing?
false
end
end

class NilClass
# No change here
def nil?
true
end

def to_s
raise "No method, old definition inconsistent."
end
end

class NoThing < NilClass
def nothing?
true
end

def empty?
true
end

def to_s
""
end

def to_i
0
end
end

class NotApplicable < NilClass
def not_applicable?
true
end
end

class Uninitialize < NilClass
def uninitialized?
true
end
end

Now if the rest of the system chose the right version of nil when it
assigned nil to something, the problem would go away and we can extend the
NoThing in a sane harm free fashion.
The problem, as others have stated, is that adding #empty? to
NilClass implies a containment. I'd have as big a problem if we
added #empty? to Fixnum so that 0 returned true and everything else
returned false. (Indeed, would that be correct? Would -3.empty? be
true or false?)

Tut! Tut! I'm having this same problem with my son at school, they teach
him the rote mechanics, but forget to teach the principles. (To be fair, I
only started learning these things when I start reading foundations of
mathematics type books from the university library...)

Come now, what is 3? 3 is 1+1+1. What do we mean by 1+1+1 we mean three
contains a 1 and a 1 and a 1! So if I extract the contents of three I get
1 and 1 and 1 back.

And -3? Surprise surprise it contains three minus ones!

There is a strong overlap between core system design and axiomatic
mathematics.

class FixNum
def empty?
self == 0
end

def length
self
end

def each
if self < 0
# -3 contains three minus ones...
(1..-self).each{|i| yield -1}
else # And 3 contains three ones.
(1..self).each{|i| yield 1}
end
end
end







John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : (e-mail address removed)
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.
 
W

William James

John said:
It's all like non-euclidean geometry. Nobody could prove Euclid's 5
postulate even though it was ugly enough to be a theorem not an axiom.

By definition, axioms are not provable.
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top