Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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