-1

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
user2049259
  • 533
  • 2
  • 6
  • 13
  • I am not sure if the question is downvote, since at least 7 of my classmates think this is extremely useful and believe the question is very clear. But is OK, finally I got the right answer in 3 minutes. – user2049259 Apr 27 '14 at 22:42

2 Answers2

2

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

flash[:notice]="the result is" + res.to_s
Zero Fiber
  • 4,417
  • 2
  • 23
  • 34
1

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

Community
  • 1
  • 1
Nimir
  • 5,727
  • 1
  • 26
  • 34