nil in evaluations

P

progcat

Problem: I have functions that sometimes evaluate to nil and cause my
program to crash in comparisons, etc.

For example g(x) and f(x) either could be nill in the following:
if g(x) > f(x)
p "not reached"
else
p "also not reached but I want it to reach here if either fuction
returns nil"
end

What can I do to make any comparison that has nil in it to evaluate to
false? I have seen operator overloading and class definitions in
ruby, so I imagine this would be quite simple.

(I know I can set g(x) and f(x) to variables before the if statement,
but I really want to avoid that)

Thanks in advance,
Tom
 
P

progcat

Close but not really what I was looking for.

example:
if g(x) > f(x)
then
put "rejoice because f(x) is greater than f(x)"
else
put "g(x) <= f(x) OR g(x) and/or f(x) returned a nil so I
wanted to execute this"
end.

In other words I want the function to work as it normally would for
valid values, and if one or both are nil I want to make the test
evaluate to false.

Sorry I was not clear.

Thanks,
Tom
 
M

Morton Goldberg

Close but not really what I was looking for.

example:
if g(x) > f(x)
then
put "rejoice because f(x) is greater than f(x)"
else
put "g(x) <= f(x) OR g(x) and/or f(x) returned a nil so I
wanted to execute this"
end.

In other words I want the function to work as it normally would for
valid values, and if one or both are nil I want to make the test
evaluate to false.

This is a case where I would use local variables (to avoid evaluating
f and g more than once). But I would reverse the test because I find
it easier to understand that way.

<code>
u, v = f(x), g(x)
if u.nil? || v.nil? || v <= u
puts "g(x) <= f(x) or g(x) and/or f(x) returned nil"
else
puts "rejoice because f(x) is greater than f(x)"
end
</code>

Why do you want to avoid using locals? There's nothing shameful in
using them.

Regards, Morton
 
R

Robert Klemme

Please don't top post.

Close but not really what I was looking for.

example:
if g(x) > f(x)
then
put "rejoice because f(x) is greater than f(x)"
else
put "g(x) <= f(x) OR g(x) and/or f(x) returned a nil so I
wanted to execute this"
end.

In other words I want the function to work as it normally would for
valid values, and if one or both are nil I want to make the test
evaluate to false.

Personally I would not mess with operators. There would be too many
places to tackle. Also this can have undesired side effects.

The main question is under which circumstances to those functions return
nil? If it is for invalid input I'd rather raise an exception
(ArgumentError) or leave the code as is (i.e. let the comparison throw)
although I'd prefer to fail fast (i.e. in those functions).

If you do not want to change your code, you can do this:
2
=> nil

I.e. use a "rescue" in the expression. The brackets are mandatory to
avoid parsing errors.

Kind regards

robert
 
R

Robert Dober

Close but not really what I was looking for.

example:
if g(x) > f(x)
then
put "rejoice because f(x) is greater than f(x)"
else
put "g(x) <= f(x) OR g(x) and/or f(x) returned a nil so I
wanted to execute this"
end.

In other words I want the function to work as it normally would for
valid values, and if one or both are nil I want to make the test
evaluate to false.
You ask nil to be greater than any other value and you ask nil to be
smaller than any other value at the same time.
I think you should rethink your modeling a bit.
Of course you can do it by defining Nil#> and redefine X#> for all X
that can be returned by g, but as Robert has pointed out correctly:
Think twice before doing this.

The quick hack if you really need it should look similar to
class Nil
def >; false end ## I guess nil is a better choice than false
end

class Integer
alias_method :eek:ld_gr, :>
def > other
return false if other.nil?
old_gr other
end
end

I you use it in production you will be bitten, be aware of it.

HTH
Robert
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top