0

I'm new to ruby and am trying to convert a string into an integer.

I am trying to calculate GPA so I am using gets for input of a letter grade (A,B,C,etc.) then I will convert each of these into their respective number grades (A=4, B=3, C=2, etc.). I have found a bunch of info on how to convert integer to strings but not strings to integer. Any suggestions?

puts ("This program will calculate your GPA this semester")    
puts ("Please type your grade, then press enter")

puts("How many courses are you taking?")
num_courses=gets.chomp
puts("Your are taking #{num_courses} courses")

puts ("Use A, A-, B+, B, B-, C+, C, C-, D, or F (Press enter after typing each grade.)")

gradeList = []
gradeList.push gets.chomp while gradeList.last != ''
puts gradeList.sort

"A"=4
"A-"=3.7
"B+"=3.3

Update: Changed code completely. I think I was coming at it from the wrong angle. However, I am still getting an error:grades.rb:10: undefined method `last' for nil:NilClass (NoMethodError)

puts "This program will calculate your GPA this semester"
puts "Please type your grade, then press enter"

puts("How many courses are you taking?")
num_courses=gets.chomp
puts("Your are taking #{num_courses} courses")

puts ("Use A, A-, B+, B, B-, C+, C, C-, D, or F (Press enter after typing each grade.)")

grade=gets.chomp while grade.last != ''

if grade == "A"
  total=total+4
elsif grade=="B"
  total=total+3
elsif grade=="C"
  total=total+2
elsif grade=="D"
  total=total+1
end

gpa=total/num_courses
puts"Your GPA is #{gpa}"
2
  • Thanks guys, I think i was coming at the code from the wrong angle and rewrote without using arrays. What do you guys think? Is the angle better now? I am still getting an error though. Check the original post for the edit and new code. Commented Jan 28, 2011 at 17:48
  • You should edit the title of this question as it appears high on search results when looking for how to cast a string to int in Ruby, but it's not about that Commented Jun 30, 2014 at 16:25

2 Answers 2

2

The reason you get that error is because grade is not defined on the first iteration. You don't realize this because you have the while positioned after the action. You should write it like this

while grade.last != '' {
  grade=gets.chomp
}

Now, other than the fact that this loop doesn't do anything that you want it to, this form is much better, because it becomes apparent that grade is nil when it evaluates.

Here is a quick re-write of your code...

puts "This program will calculate your GPA this semester"

puts "How many courses are you taking?"
num_courses = gets.chomp.to_i                    # num_courses is now an integer
puts "You are taking #{num_courses} courses"     # let's pretend num_courses = 4

puts "Use A, A-, B+, B, B-, C+, C, C-, D, or F"

values = {                          # using a hash will allow us to avoid a
    "A" => 4,                       # large and inefficient if / elsif statement
    "A-" => 3.7,
    "B+" => 3.3,
    "B" => 3,
}

total = 0.0                         # sets our total prior to the loop for scope
num_courses.times do                # so we will do this loop 4 times
    total += values[gets.chomp.upcase]       # looks up the value from our hash
end                                          # and adds it to the (running) total

gpa = total / num_courses           # calculates the gpa from the total
                                    # and the num_courses we asked earlier
puts "Your GPA is #{gpa}"

There are a few other ways you could do some of this, but hopefully the above is simple enough that you see the general concepts that you may have been struggling to grasp before.

I hope this helps you, but ask anything you might still wonder.

Sign up to request clarification or add additional context in comments.

2 Comments

This code is a little brittle, that is it will fail if anything that does not match the grades in the value hash are entered. But it will suffice for what appears to be a simple homework assignment. @Faisal, there is a syntax error in the code above, but you should be able to find and fix it.
@Steve I am aware of the "brittle-ness" of this, but I thought that I would leave that for Faisel once he understood what he was doing wrong and a better way of how to do it. Re: the syntax error, I am failing to see anything other than the silly gets.chomp.to_i which could just be gets.to_i. What are you referring to?
2

The simplest solution here would probably be a hash:

GRADE_VALUES = {
  "A" => 4,
  "A-" => 3.7,
  ...
}

gradeList = []
gradeList.push GRADE_VALUES[gets.chomp.strip] while gradeList.last != ''


puts gradeList

=> [3.7, 4, 3.3 ... ]

2 Comments

thanks man, you think you could help with the new code i put in the edit?
GRADE_VALUES[gets.chomp.strip] will push a nil to the end of the gradeList when you do a GRADE_VALUES['']. The loop you have written will never return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.