新手问题 Ruby 如何实现类似 Python, Haskell... import 的效果?

Alexander · March 09, 2013 · Last by valentine replied at March 09, 2013 · 2733 hits

简单说来,就是实现 Pythonfrom Foo import Bar 的效果,谢谢啦!

假如 foo.rb 里定义了 Bar, require './foo' 就能用 Bar 了

#1 楼 @luikore 我没有描述清楚,我想要的是,在 foo.rb 中有一个模块 Foo, Foo 中有类 Bar0, Bar1, 我想直接通过 Bar0Foo::Bar0, 如果 include Foo 的话,它同时也把 Bar1 引入了; 我的问题就是只在当前名称域 (我不知道术语是什么,不过你懂我的吧:-).) 里面引入 Bar0 而不引入 Bar1Foo(可选).

#2 楼 @Alexander 可以这么写:Bar0 = Foo::Bar0 ... 但是 Foo 还是引入了。如果不怕 warning 的话,可以接一个 Foo = nil ... 但这不是 Ruby 的用法... ruby module 的组织方式和想法和 python 完全不一样的

这完全是两种风格啊。Ruby 那 require 就好像 C 语言的#include,Python 的 import 分明是文件系统的符号链接

#3 楼 @luikore #4 楼 @bhuztez 多谢两位,我也就随便问问,^_^

python 的方式弱爆了

You need to Sign in before reply, if you don't have an account, please Sign up first.