I have this simple method
def is_palindrome?(sentence)
raise ArgumentError.new('expected string') unless sentence.is_a?(String)
safe_sentence = sentence.gsub(/\W+/, '').downcase
return safe_sentence == safe_sentence.reverse
end
is_palindrome?"rails"
When I run it I get the error void value expression
in line 4 which is the return statement
What's wrong here?
I know this is long overdue but I noticed that this question didn't have an answer, as I was researching this exact same problem. I got the same kind of error not to long ago, what this error means is that Ruby is trying find a conditions change
within your code and can't find it. This could be because Ruby isn't reading your code properly, or it could be because there's a bug somewhere. Fixing it is actually extremely easy.
def is_palindrome?(sentence)
raise ArgumentError.new('expected string') unless sentence.is_a?(String)#<= Right here at 'unless' condition is changed
safe_sentence = sentence.gsub(/\W+/, '').downcase
return safe_sentence == safe_sentence.reverse
end
is_palindrome?"rails"
For some reason your version of Ruby is trying to find the condition change and defaulting to the original condition, it took a lot of research to come up with this.
So how do you fix this? The quick answer is to add an if/else statement
instead of an unless
:
def is_palindrome?(word)
to_rev = word.split('')
rev_arr = to_rev.reverse
new_word = rev_arr.join('')
if new_word == word
return true
else
return false
end
end
is_palindrome?("racecar") #<= Returns true
is_palindrome?("test") #<= Returns false
If you wanted to use your regex:
def is_palindrome?(sentence)
new_sentence = sentence.gsub(/\W+/, '').downcase.reverse
if new_sentence == sentence.downcase
return true
elsif new_sentence != sentence.downcase
return false
else
raise ArgumentError.new("String expected, instead found #{sentence}")
end
end
is_palindrome?("racecar") #<= Returns true
is_palindrome?("test") <= Returns false
This is completely untested but you get the idea. This will add a change in conditions and will allow Ruby to realize that there is a change in the conditions.
Now why is your system outputting this? The short answer again is, nobody really knows, could be your system has a bug in it, could be that your version of Ruby somewhere is corrupted.
A couple things you can try:
The reason people are unable to reproduce this error is probably because nobody can really see the way you're running the program, maybe you have a different version of Linux, or Windows, whatever. Hope this answered your question..
I came to this question when I was trying to return evaluated boolean in return statement:
return x==1 or x==2
It turns out that ruby need brackets:
return (x==1 or x==2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With