다른 클래스를 반환하려면 클래스가 필요하지만 그렇게하지는 마십시오. 왜 그런지 설명해 주시겠습니까? 여기 나는 그 것이 부적절하다고 생각하므로 Temperature
클래스를 포함하지 않습니다.클래스 및 클래스 반환 변수
class Celsius
attr_accessor :temp
def initialize(cel)
return Temperature.new({:c =>cel})
end
end
class Fahrenheit
def initialize (far)
Temperature.new({:f => far})
end
end
나는 내가 return
를 사용하려하지만 도움이되지 않았다, Celsius
에서 Temperature
클래스를
puts Fahrenheit.new(0) #=> returns Fahrenheit class instead of Temperature
를 반환 할 Fahrenheit
클래스를 호출하고 싶습니다. ::new
consctuctor이 Temperature
을 지정된 클래스의 새로 생성 된 인스턴스를 반환하기 때문에 대신 온도의
(!) 참고 부탁했다 : 당신은 왜'화씨 :: Temperature'을하지? – rlecaro2
[this] (http://stackoverflow.com/questions/21663548/ruby-instance-methods-and-variables/21667363) 질문 후에도 여전히 혼란스러워합니다. 당신은 * 클래스 *를 반환하려고하지 않고, 다른 클래스의 인스턴스 *를 반환하려고합니다. 큰 차이가 있습니다. – Max
어떻게 불리나요? – Grag808