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

program.rb
string = "123"
# YOUR CODE HERE
string = integer.to_i
string = float.to_f

1 Answer

Rogier Nitschelm
Rogier Nitschelm
iOS Development Techdegree Student 5,460 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.

Perhaps:

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

?