-2

I wrote the following code in Ruby for Celsius to Fahrenheit conversion. I keep getting error. I am sure that I am still not clear on the concept of methods and that's why I cant figure it out.

puts "Enter the Degree in c:"
c = gets.chomp

def celsius_fahrenheit (f) 
  return f = ( c * 9 / 5) + 32
end

answer = "The #{celsius_fahrenheit (f)} equivalent is"
puts answer
Marco
  • 2,007
  • 17
  • 28
  • It's helpful to include the error you're getting. – Dave Newton Sep 12 '17 at 19:47
  • 1
    I don't think Ruby (or any other common developer toolchain) uses secret errors (*Psst! You have an error, but I won't tell you what it is. Pass it on!*). You clearly got an error message, or you wouldn't know you had an error. That message is on the screen, right in front of you. There is absolutely no reason for you to fail to include that error message in your question here. You're asking for *free help* to solve *your problem*. The very least you should do is give us the relevant details that are right on the screen in front of you to use to help. – Ken White Sep 12 '17 at 19:51
  • Just at a *glance*, it looks like you're dividing two ints together. You need one of those to be a float so you get 1.8 * c. – Makoto Sep 12 '17 at 19:55
  • Sorry , I forgot the error : – Moh Reza KH Sep 12 '17 at 19:57
  • This is the error :challenge.rb:12:in `
    ': undefined local variable or method `f' for main:Object (NameError)
    – Moh Reza KH Sep 12 '17 at 19:57
  • Rather than post your error message in the comments, edit your question with it. Thanks – Sagar Pandya Sep 12 '17 at 20:02
  • @sagarpandya82 sorry , I am new to whole coding and using this website :) i will keep in mind for future ! – Moh Reza KH Sep 12 '17 at 20:18

1 Answers1

3

You have several problems:

  1. convert c to Float
  2. change signature of celsius_fahrenheit to use c as a parameter
  3. don't do an assignment in the method's body

Here is the code:

def celsius_fahrenheit(c)
  c * 9 / 5 + 32
end

puts 'Enter C:'
c = gets.to_f
puts celsius_fahrenheit(c)
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79