Question

I want to do something like this:

in controller:

res=1+3
if ...
flash[:notice]="the result is" + res
end

However, I found that it does not work, and the error message is like this:

no implicit conversion of Fixnum into String
Was it helpful?

Solution

You need to convert it to the res variable which is an integer to a string:

flash[:notice]="the result is" + res.to_s

OTHER TIPS

I would do it using string interpolation:

flash[:notice] = "the result is #{res}"

Which is better than concatenation (using + ) because:

  1. Its faster
  2. As you can see it automatically do .to_s for you
  3. Less typing (developers are lazy ofcourse)

See this SO question for more details and comparison

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top