Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Basics Numbers Converting Strings to Numbers

Saadia Fattah
Saadia Fattah
1,939 Points

I don't understand why this says this

We expected the "integer" variable to equal 123, but it was: nil

string = "123"
string = integer.to_i
string = float.to_f

1 Answer

Rogier Nitschelm
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

Take a look at the message. It states that the integer variable is supposed to equal 123. But apparently it does not.

The two questions you could ask are:

  1. Has a variable named integer been declared?
  2. If yes, have I assigned a value of 123 to it?

string = integer.to_i

Here you are assigning the result of integer.to_i to a variable named string. But what I think you want to do is assign the result of string.to_i to a variable named integer.


string = "123"
integer = string.to_i # "123" => 123