You should be careful about the processing before returning.
example
For example, when implementing the following method
p sample_method()
# => { 'hoge' => 0, 'piyo' => 0 }
If you mistakenly describe the process before return, a syntax error will occur when using return, but
def sample_method
result = { 'hoge' => 0 }
#Wrong comma after 1","I have attached
result['piyo'] = 1,
return result
end
# =>
# SyntaxError (xxx:nn: void value expression)
# return result
# ^~~~~~
# xxx:nn: syntax error, unexpected local variable or method, expecting `end'
# return result
# ^~~~~~
If return is omitted, an unintended value may be returned.
def sample_method
result = { 'hoge' => 0 }
#Wrong comma after 1","I have attached
result['piyo'] = 1,
result
end
p sample_method()
# => [1, {"hoge"=>0, "piyo"=>[...]}]
The syntax is correct for Ruby, so no error occurs.
Recommended Posts