[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

nil in evaluations

progcat

4/18/2007 2:08:00 AM



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

5 Answers

Prime

4/18/2007 2:43:00 AM

0

I am not sure this is what you wanted but,

if g(x).nil? || f(x).nil?
p "nil found"
else
p "no nil"
end

can be used to evaluate if the function equals nil



On 2007-04-17 19:08:05 -0700, progcat@comcast.net said:

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


progcat

4/18/2007 3:08:00 AM

0

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

On Apr 17, 9:42 pm, Prime wrote:
> I am not sure this is what you wanted but,
>
> if g(x).nil? || f(x).nil?
> p "nil found"
> else
> p "no nil"
> end
>
> can be used to evaluate if the function equals nil
>
> On 2007-04-17 19:08:05 -0700, prog...@comcast.net said:
>
>
>
> > 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


Morton Goldberg

4/18/2007 4:12:00 AM

0

On Apr 17, 2007, at 11:10 PM, progcat@comcast.net wrote:

> 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

Robert Klemme

4/18/2007 8:00:00 AM

0


Please don't top post.

On 18.04.2007 05:08, progcat@comcast.net wrote:
> 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:

>> if (4 > nil rescue false)
>> p 1
>> else
?> p 2
>> end
2
=> nil

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

Kind regards

robert

Robert Dober

4/18/2007 9:57:00 AM

0

On 4/18/07, progcat@comcast.net <progcat@comcast.net> wrote:
> 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 :old_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
>
> Sorry I was not clear.
>
> Thanks,
> Tom
>

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw