When do Ruby instance variables get set?

前端 未结 6 1687
盖世英雄少女心
盖世英雄少女心 2020-12-02 04:42
class Hello
@hello = \"hello\"
    def display
        puts @hello
    end
end

h = Hello.new
h.display

I created the class above. It doesn\'t prin

6条回答
  •  悲哀的现实
    2020-12-02 05:37

    You need to add an initialize method:

    class Hello
        def initialize
            @hello = "hello"
        end
        def display
            puts @hello
        end
    end
    
    h = Hello.new
    h.display
    

提交回复
热议问题