• 关于load和require的补充

    Speaking of Namespaces (23), there is one interesting detail that involves Namespaces, constants, and Ruby’s load and require methods. Imagine finding a motd.rb file on the web that displays a “message of the day” on the console. You want to add this code to your latest program, so you load the file to execute it and display the message:

    load('motd.rb')

    Using load, however, has a side effect. The motd.rb file probably defines variables and classes. Although variables fall out of scope when the file has finished loading, constants don’t. As a result, motd.rb can pollute your program with the names of its own constants—in particular, class names.

    You can force motd.rb to keep its constants to itself by passing a second, optional argument to load:

    load('motd.rb', true)

    If you load a file this way, Ruby creates an anonymous module, uses that module as a Namespace to contain all the constants from motd.rb, and then destroys the module.

    The require method is quite similar to load, but it’s meant for a different purpose. You use load to execute code, and you use require to import libraries. That’s why require has no second argument: those leftover class names are probably the reason why you imported the file in the first place. Also, that’s why require tries only once to load each file, while load executes the file again every time you call it.

  • 不能发帖了? at 2018年04月18日

    手机端怪怪的,不过配色感觉更时尚了哈哈

  • (what happens at Ekohe, stays at Ekohe)

  • 我就是想请教你能不能说的具体点,你认为的基础是什么,指针,oop? 我转行程序员,即使学java也一样会被说基础不好,跟语言有关吗?

  • 请问差在哪里呢?不知道scope, 不知道pass by reference/value这些概念的那种差?

  • 恭喜你,这是珍藏版!

  • https://ruby-china.org/topics/34907这个帖子是我心病,一直没抽时间解决

  • 可以网络面试吗? at 2018年03月05日

    谢谢,已加

  • 可以网络面试吗? at 2018年03月05日

    谢谢,不过网面也可以做到您说的考察我的素质呀,用软件分享屏幕,一边解题一遍说思路,解题过程完全可以展现出来呀

    不过我发现我经历的几个网面的确只是单单视频和电话面试,但是这样也可以不错地考察到我的水平呀,而且我的代码也在github上。可能国内的网面视频电话的方式比较多吧