Page 1 of 1

how to get rid of ruby error for nil class on start

Posted: Sun Jul 24, 2016 10:16 am
by tester
On several occasions, my ruby modules have nil inputs when starting, which produces some errors (see on load and when clicking). See on attached example. My question - how to get rid of this problem from within ruby module?

Re: how to get rid of ruby error for nil class on start

Posted: Sun Jul 24, 2016 1:23 pm
by adamszabo
Maybe connect an afterload trigger there?

Re: how to get rid of ruby error for nil class on start

Posted: Sun Jul 24, 2016 1:38 pm
by tester
It's not the matter of afterload trigger.

What I attached - is an exmple schematic of what happens when such ruby module receives nil value.

What I need, is to avoid such problems from within ruby module/code.

Re: how to get rid of ruby error for nil class on start

Posted: Sun Jul 24, 2016 1:53 pm
by Nubeat7
just call methodes when no of the required parameters are nil,
avoid nil values to happen,
or set nils to valid values

Re: how to get rid of ruby error for nil class on start

Posted: Sun Jul 24, 2016 5:06 pm
by tulamide
What Nubeat says, esp. the last one. NIL informs you that you try to work with a non-existing value. It's an essential tool to avoid errors. So test for nil, before proceeding. Typical examples:

Code: Select all

def mysquare(value)
    return value**2 unless value.nil?
end

def count_existing(value)
    mycounter += 1 if value
end

def accessing_an_input
    return if @ins[0].nil?
    ##safely use first input from here on##
end


Adam has a point though. If you have inputs that can be nil, but you don't want them to be nil (because your code doesn't even recognize nil), you should indeed set them to values after start/duplicating, etc.

Re: how to get rid of ruby error for nil class on start

Posted: Sun Jul 24, 2016 8:54 pm
by tester
Nubeat7 wrote:just call methodes when no of the required parameters are nil,
avoid nil values to happen,
or set nils to valid values


It's not that simple. On several occasions, some things are reset, and nil out with trigger passes through. It's one of these monster-bigbig projects...

Right now I'm using something like this:

--------------------------------
def event(in_id, val)
if in_id == "trigger"

anything else

end
end
--------------------------------

where trigger comes from a separate input.

What I need to add is just something, that detects if inputs are nil - and instead of producing error code - just sends nil (with trigger or without it - passive way) through/to associated outputs.

Re: how to get rid of ruby error for nil class on start

Posted: Sun Jul 24, 2016 9:55 pm
by Nubeat7
in general i mostly avoid nil, except for sending some triggersignals to an output...
the handling of nils can be very different, so its not that easy to give an "always working solution"

but mostly its some asking if its nil then react like tulamides examples

but you can also work the other way and only do something if a value is a specific value, so instead of asking for nil you can ask for string or float before doing something

btw. sometimes its also important to make a difference between empty and nil..

Re: how to get rid of ruby error for nil class on start

Posted: Mon Jul 25, 2016 10:23 am
by billv
Nil errors were always the most annoying part of FSRuby.
To fix this issue in all major Ruby jobs, i came up with this method a few
years ago, to silence any threat from a nil error 'stealth attack'.
Create the following code block after the 'def init' section.

def nil errors
Insert your condoms and devices of protection here
end

Then just write " nil errors " at the start of every code block.

Works great for me.But thats just me, and therefore has no real meaning. :D :lol:

Have a nice day!