Opposite .nil?

I've looked around, but could not find a method that is the opposite of
.nil?. I know there is !my_var.nil?, but I think my_var.exists? is more
readable.

I wanted to throw this out there before I added the method in myself.

thanks,
andy

···

--
Andrew Stone

Andrew Stone wrote:

I've looked around, but could not find a method that is the opposite of
.nil?. I know there is !my_var.nil?, but I think my_var.exists? is more
readable.

I wanted to throw this out there before I added the method in myself.

thanks,
andy

if not my_var.nil?

is the same as

if my_var

_unless_ my_var is false.

-Justin

if not my_var.nil?

Thanks Justin. I should have been more clear with the usage.

def should_this_be_done?
  am_I_sure? && my_object.var && !my_object.var.nil?
end

I just think it would read better if the last test was my_object.var.exists?

thanks,
andy

···

--
Andrew Stone

def should_this_be_done?
  am_I_sure? && my_object.var && !my_object.var.nil?
end

Ooops... meant

def should_this_be_done?
  am_I_sure? && my_object && !my_object.var.nil?
end

Didn't mean to put add .var on the second test.

···

--
Andrew Stone

There's no reason to say myobject.var && !my.object.var.nil? Any
object that passes the first test is not nil.

The only reason to check !obj.nil? is if you want false to be an
accepted value.

···

On Dec 11, 12:56 pm, "Andrew Stone" <stoneli...@gmail.com> wrote:

if not my_var.nil?

Thanks Justin. I should have been more clear with the usage.

def should_this_be_done?
  am_I_sure? && my_object.var && !my_object.var.nil?
end

I just think it would read better if the last test was my_object.var.exists?

thanks,
andy
--
Andrew Stone

--
-yossef

1) No such opposite-of-nil? method exists. If you want it, add it.

class Object
  def exists?
    true
  end
end

class NilClass
  def exists?
    false
  end
end

# up to you if you want this for FalseClass, too

2) In the particular example above (which I realize is just an
example) you could write that as:

  def should_this_by_done?
    am_I_sure? unless !my_object || my_object.var.nil?
  end

But, of course, that just moves the negation.

···

On Dec 11, 11:56 am, Andrew Stone <stoneli...@gmail.com> wrote:

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

if not my_var.nil?

Thanks Justin. I should have been more clear with the usage.

def should_this_be_done?
  am_I_sure? && my_object.var && !my_object.var.nil?
end

I just think it would read better if the last test was my_object.var.exists?

There's no reason to say myobject.var && !my.object.var.nil? Any
object that passes the first test is not nil.

Please see the followup email I sent.

thanks,
andy

···

--
Andrew Stone

1) No such opposite-of-nil? method exists. If you want it, add it.

Thanks for actually answering my question. Much appreciated.

2) In the particular example above (which I realize is just an

example) you could write that as:

def should_this_by_done?
   am_I_sure? unless !my_object || my_object.var.nil?
end

But, of course, that just moves the negation.

Sure, I was just looking for a more readable construct. Thanks again for
your reply.

···

--
Andrew Stone

Well, you could do

am_I_sure? && my_object && my_object.var

It's not exactly the same but if you don't care whether my_object.var is nil or false or if you know that it never will be false then it's ok.

Kind regards

  robert

···

On 11.12.2007 20:00, Andrew Stone wrote:

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

def should_this_be_done?
  am_I_sure? && my_object.var && !my_object.var.nil?
end

Ooops... meant

def should_this_be_done?
  am_I_sure? && my_object && !my_object.var.nil?
end

Didn't mean to put add .var on the second test.

I'd suggest not_nil?

class Object
    def not_nil?
       !self.nil?
    end
end

exists? or not_nil? are both awkward if you actually have
a reference to false:

     condition = (2 > 3) # condition is false

     if condition.exists?
        # this branch will run because condition is not nil
     else
        # do something else
     end

I think it would be clearer to be explicit about your intent
if you really want to bundle up false with true-behaving
objects:

     if condition or (condition == false)
       # do something
     end

In practice, I've rarely come across a situation where I wanted
to treat a false reference the same as a reference to a 'real'
object.

Gary Wright

···

On Dec 11, 2007, at 3:03 PM, Andrew Stone wrote:

Sure, I was just looking for a more readable construct. Thanks again for
your reply.

def should_this_be_done?
  am_I_sure? && my_object && !my_object.var.nil?
end

Here's a more readable construct:

def should_this_be_done?
  am_I_sure? and my_object and not my_object.var.nil?
end

You're not forced to program Ruby like it's C.

--Ken

···

On Tue, 11 Dec 2007 15:03:53 -0500, Andrew Stone wrote:

Note: parts of this message were removed by the gateway to make it a
legal Usenet post.

1) No such opposite-of-nil? method exists. If you want it, add it.

Thanks for actually answering my question. Much appreciated.

2) In the particular example above (which I realize is just an

example) you could write that as:

def should_this_by_done?
   am_I_sure? unless !my_object || my_object.var.nil?
end

But, of course, that just moves the negation.

Sure, I was just looking for a more readable construct. Thanks again
for your reply.

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

what about

def should_this_be_done?
  am_1_sure? && my_object.var && my_object.var
end

KISS

···

On Dec 11, 3:03 pm, Andrew Stone <stoneli...@gmail.com> wrote:

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

> 1) No such opposite-of-nil? method exists. If you want it, add it.

Thanks for actually answering my question. Much appreciated.

2) In the particular example above (which I realize is just an> example) you could write that as:

> def should_this_by_done?
> am_I_sure? unless !my_object || my_object.var.nil?
> end

> But, of course, that just moves the negation.

Sure, I was just looking for a more readable construct. Thanks again for
your reply.

--
Andrew Stone

I definitely wanted a true opposite of .nil? and I do agree the term exists?
is a little awkward, but it was the first term that people seem to use when
thinking of !nil?. btw, this was not the result of some huge internet poll
with thousands of responses, just 4 people, so...eh, it was good enough for
me.

Phrogz mentioned the possibility of adding it to the FalseClass as well, but
that's not my intention for this method. False is a value just like an
empty string is a value where nil is the absence of value. Now I know some
will argue that the absence of a value is a value. True and fair enough and
a reason why the exists? method wouldn't be added to the False class to
return a false value. false.exists? == true.

I don't run across this scenario often. Today was just one of those days
that I decided to look a little further into the possibilities.

Gary and Robert, thanks for the responses,
andy

···

On Dec 11, 2007 6:27 PM, Gary Wright <gwtmp01@mac.com> wrote:

On Dec 11, 2007, at 3:03 PM, Andrew Stone wrote:
> Sure, I was just looking for a more readable construct. Thanks
> again for
> your reply.

I'd suggest not_nil?

class Object
   def not_nil?
      !self.nil?
   end
end

exists? or not_nil? are both awkward if you actually have
a reference to false:

    condition = (2 > 3) # condition is false

    if condition.exists?
       # this branch will run because condition is not nil
    else
       # do something else
    end

I think it would be clearer to be explicit about your intent
if you really want to bundle up false with true-behaving
objects:

    if condition or (condition == false)
      # do something
    end

In practice, I've rarely come across a situation where I wanted
to treat a false reference the same as a reference to a 'real'
object.

Gary Wright

--
Andrew Stone

:slight_smile: very true Ken. I come from Java land myself and I guess old habits die
hard.

thanks for the response,
andy

···

On Dec 11, 2007 7:10 PM, Ken Bloom <kbloom@gmail.com> wrote:

On Tue, 11 Dec 2007 15:03:53 -0500, Andrew Stone wrote:

> Note: parts of this message were removed by the gateway to make it a
> legal Usenet post.
>
>> 1) No such opposite-of-nil? method exists. If you want it, add it.
>>
>>
> Thanks for actually answering my question. Much appreciated.
>
> 2) In the particular example above (which I realize is just an
>> example) you could write that as:
>>
>> def should_this_by_done?
>> am_I_sure? unless !my_object || my_object.var.nil?
>> end
>>
>> But, of course, that just moves the negation.
>>
>>
> Sure, I was just looking for a more readable construct. Thanks again
> for your reply.

def should_this_be_done?
am_I_sure? && my_object && !my_object.var.nil?
end

Here's a more readable construct:

def should_this_be_done?
am_I_sure? and my_object and not my_object.var.nil?
end

You're not forced to program Ruby like it's C.

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/ <http://www.iit.edu/~kbloom1/&gt;

--
Andrew Stone

Here's a more readable construct:

Hmm... maybe 'readable' is in the eye of the beholder :wink:

def should_this_be_done?
  am_I_sure? and my_object and not my_object.var.nil?
end

Folks should be aware of the difference in precedence. In particular,
&& has a higher precedence than =, but 'and' has a lower precedence
than =.

You're not forced to program Ruby like it's C.

Agreed

···

On Dec 11, 7:02 pm, Ken Bloom <kbl...@gmail.com> wrote:

--Ken

Facets has #non_nil? and #not_nil?. It also has #val? for not nil or
false or not empty if enumerable. Not to mention #false? and #true?

T.