My understanding is that ruby returns the last statement evaluated in a function. What if the function ends with an if statement that evaluates to false
def thing(input)
item = input == "hi"
if item
[]
end
end
puts thing("hi").class #> Array
puts thing("not hi").class #> NilClass
I like this functionality (returning nil if the statement is false), but why isn’t false returned (from the assignment to item)?
If your
ifstatement doesn’t result in any code being run, it returnsnil, otherwise it returns the value of the code that was run.irbis a good tool to experiment with such stuff: