在Ruby中,是否有类似的;导入。。。“作为……”;用Python?

在Ruby中,是否有类似的;导入。。。“作为……”;用Python?,ruby,alias,Ruby,Alias,在Python中,我可以导入模块的一个子集,给它一个短名称,并从代码中只引用短名称。e、 g import bigmodule.bigobject.somethingbig as cutie ruby中是否有类似的东西,这样我就不必键入长字符串了?ruby中没有(import..as..)这样的东西,但是您可以通过不同的方法轻松访问该方法 ruby中的一切都是一个对象,可以像其他对象一样分配给另一个变量。您只需将子模块分配给一个变量,就可以将该变量视为子模块引用 # Assume you ha

在Python中,我可以导入模块的一个子集,给它一个短名称,并从代码中只引用短名称。e、 g

import bigmodule.bigobject.somethingbig as cutie
ruby中是否有类似的东西,这样我就不必键入长字符串了?

ruby中没有(
import..as..
)这样的东西,但是您可以通过不同的方法轻松访问该方法

ruby中的一切都是一个对象,可以像其他对象一样分配给另一个变量。您只需将子模块分配给一个变量,就可以将该变量视为子模块引用

# Assume you have followed the rails conventions for naming and file structure
# 'bigmodule/bigobject/somethingbig'
cutie = Bigmodule::Bigobject::Something

# Now you can access module methods with `cutie` reference variable.
# You can also define a method, which access the submodule
def cutie
  Bigmodule::Bigobject::Something
end
例如:

# Assigning String class to a variable
a = String
a.new("Hello World") # It will produce a new string with value as 'Hello World'
Ruby中没有这样的东西(
import..as..
),但是您可以通过不同的方法轻松访问该方法

ruby中的一切都是一个对象,可以像其他对象一样分配给另一个变量。您只需将子模块分配给一个变量,就可以将该变量视为子模块引用

# Assume you have followed the rails conventions for naming and file structure
# 'bigmodule/bigobject/somethingbig'
cutie = Bigmodule::Bigobject::Something

# Now you can access module methods with `cutie` reference variable.
# You can also define a method, which access the submodule
def cutie
  Bigmodule::Bigobject::Something
end
例如:

# Assigning String class to a variable
a = String
a.new("Hello World") # It will produce a new string with value as 'Hello World'

没有。如果你要从python到Ruby,这是你会习惯的事情之一。您将了解到,以python的方式进行操作是毫无意义的


但是你有没有一个你不喜欢的代码的例子?也许有更好的办法。

没有。如果你要从python到Ruby,这是你会习惯的事情之一。您将了解到,以python的方式进行操作是毫无意义的


但是你有没有一个你不喜欢的代码的例子?可能有更好的方法。

我不认为您可以别名模块,如果它非常长并且在多个地方使用,您可以定义一个函数来公开该模块,或者您可以将其插入初始化方法并用作实例变量。我不认为您可以别名模块,如果它真的很长并且在多个地方使用,您可以定义一个公开该模块的函数,或者您可以将它注入到您的initialize方法中并用作实例变量