nil in evaluations

Discussion in 'Ruby' started by progcat@comcast.net, Apr 18, 2007.

  1. Guest

    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
     
    , Apr 18, 2007
    #1
    1. Advertising

  2. Guest

    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, 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
     
    , Apr 18, 2007
    #2
    1. Advertising

  3. On Apr 17, 2007, at 11:10 PM, 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
     
    Morton Goldberg, Apr 18, 2007
    #3
  4. Please don't top post.

    On 18.04.2007 05:08, 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 Klemme, Apr 18, 2007
    #4
  5. Robert Dober Guest

    On 4/18/07, <> 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 :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
    >
    > 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
     
    Robert Dober, Apr 18, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candler

    puts nil generates "nil\n"

    Brian Candler, Nov 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    124
  2. John Carter
    Replies:
    64
    Views:
    697
    Klaus Stein
    May 19, 2005
  3. ako...

    a == nil or a.nil?

    ako..., Nov 22, 2005, in forum: Ruby
    Replies:
    6
    Views:
    168
    Douglas Livingstone
    Nov 23, 2005
  4. Christoffer Sawicki
    Replies:
    5
    Views:
    287
    Christoffer Sawicki
    Sep 2, 2006
  5. Eero Saynatkari

    nil.to_s != "nil"

    Eero Saynatkari, Oct 27, 2006, in forum: Ruby
    Replies:
    11
    Views:
    254
    Gregory Brown
    Oct 28, 2006
Loading...

Share This Page