I'm wondering why this is so: Ruby concatenates two strings if there is a space between the plus and the next string. But if there is no space, does it apply some unary operator?
params['controller'].to_s + '/'
# => "posts/"
params['controller'].to_s +'/'
# => NoMethodError: undefined method `+@' for "/":String
The parser is interpreting +'/'
as the first parameter to the to_s
method call. It is treating these two statements as equivalent:
> params['controller'].to_s +'/'
# NoMethodError: undefined method `+@' for "/":String
> params['controller'].to_s(+'/')
# NoMethodError: undefined method `+@' for "/":String
If you explicitly include the parenthesis at the end of the to_s
method call the problem goes away:
> params['controller'].to_s() +'/'
=> "posts/"
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