Return values from a function - basic question

I thought this would be simple, but I just cant seem to get it to work.

only allow numbers. Need to check for nil / NAN

def saveHours(val)
if val < 0
val = 0
end
return val
end

current_item.product.v1 = checkValue(values[:val1])
current_item.product.v2 = checkValue(values[:val2])

but when I run the code, nothing is ever returned from the checkValue
function. If I remove the function call and just have values[:val1]
things work fine.

Ive also tried removing the return and just having ‘val’ as the last
function line, but I get the same result. I tried placing this function
in a helper file and in the same file that the other code is in, but no
luck.

Any ideas?

Has anyone used functions to return a value before? I could jump
through a ton of hoops to get this to work, - like manually checking
each input or setting up individual local vars, but thought there might
be a more elegant way to do this.

thanks.

Bob wrote:

I thought this would be simple, but I just cant seem to get it to work.

only allow numbers. Need to check for nil / NAN

def saveHours(val)
if val < 0
val = 0
end
return val
end

current_item.product.v1 = checkValue(values[:val1])
current_item.product.v2 = checkValue(values[:val2])

but when I run the code, nothing is ever returned from the checkValue
function. If I remove the function call and just have values[:val1]
things work fine.

Ive also tried removing the return and just having ‘val’ as the last
function line, but I get the same result. I tried placing this function
in a helper file and in the same file that the other code is in, but no
luck.

Any ideas?

Never mind - the function wasn’t working because the web input was a
string. I tried a regex instead of a straight compairson and it worked
fine.

Sorry for bugging.

Bob

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs