0

How can I print this method from the class?

I'm new to Ruby so I'm not sure who to print this. I essentially need to enter a number into this method and print.

Similar to a JS function, I know I must use puts, but that's pretty much about it.

class Celcius
  def initialize(temperature)
    @temperature = temperature
  end

  def fahrenheit
    (@temperature * 1.8 + 32).round
  end

  def to_s
    "#{@temperature} degrees C"
  end
end 
1
  • 3
    Please post code as plain text in the body of your question. Adding it as an image makes it very difficult for people to adapt your code to an answer, you can't copy-paste it, and it's hostile to those dependent on screen-readers. Commented Jul 20, 2016 at 19:34

1 Answer 1

4

Whenever you need to print something:

temp = Celcius.new(120)

puts temp.to_s

Now puts likes to convert things to string, and to_s is the default way of doing this, so this should work:

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

Comments