There is something that i don't understand about ruby class instance variable or methods**. So i have this code that keeps on giving me this error and i cant understand
Looks ruby thinks that i am trying to call for Float.in_celsius but I want to make this call within my class instance.
#-----------------------------------
def ftoc(fr)
fr = fr.to_f
if (fr == 32)
c = 0
elsif (fr == 212)
c = 100
else
c = (fr-32.0)*(5.0/9.0)
end
return c
end
def ctof (cl)
cl = cl.to_f
f = (cl*(9.0/5.0))+32.0
return f
end
#-----------------------------------
class Temperature
attr_accessor :in_celsius, :in_fahrenheit
#class metods
def self.from_celsius(cel)
puts "from celsious\n"
puts "cel: #{cel}\n"
@in_fahrenheit = cel
@in_celsius = ctof(cel)
puts "==============================\n"
return @in_celsius
end
def self.in_celsius
@in_celsius
end
end
puts "==============================\n"
puts Temperature.from_celsius(50).in_celsius
puts Temperature.from_celsius(50).in_fahrenheit
and Error is
test.rb:54: in '<main>' : undefined method 'in_celsius' for 122.0:float (noMethod Error)
enter code here
ftoc(fr)todef ftoc(fr) (fr-32.0)*(5.0/9.0) end. You don't need to convertfrto a float, becausefr-32.0will do that, and you don't needreturnbecause Ruby returns the last calculation performed. Same forctof().