How can I access a variable defined in a Ruby file I required in IRB?

前端 未结 4 1692
囚心锁ツ
囚心锁ツ 2020-12-01 18:33

The file welcome.rb contains:

welcome_message = \"hi there\"

But in IRB, I can\'t access the variable I just created:

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 19:01

    You can't access local variables defined in the included file. You can use ivars:

    # in welcome.rb
    @welcome_message = 'hi there!'
    
    # and then, in irb:
    require 'welcome'
    puts @welcome_message
    #=>hi there!
    

提交回复
热议问题