简体   繁体   English

如何将Ruby的模块移植到Python

[英]How to port a module for Ruby to Python

Or how can I learn Ruby quickly? 或者如何快速学习Ruby? Is there a pattern to port a ruby module to python? 是否存在将ruby模块移植到python的模式?

I want to add scope system to Sass, but I know little about ruby, and when I started, I found the contents of a module are in several files, there are a lot of variables or classes undefined but referenced. 我想将范围系统添加到Sass,但是我对ruby知之甚少,当我开始时,我发现模块的内容在几个文件中,有很多未定义但被引用的变量或类。

What's the difference between require keyword and the python's import? require关键字和python的import之间有什么区别?

Or at least, is there a ruby IDE that can tell me where is a module, class, variable defined like PyDev does? 或者至少,有一个ruby IDE可以告诉我像PyDev这样定义的模块,类,变量在哪里?

There probably are IDE out there that have a bit of code help built in, but I usually don't find I need it. 那里可能有一些内置了代码帮助的IDE,但是我通常不觉得我需要它。 Most Ruby libraries have a certain structural pattern to them. 大多数Ruby库都具有一定的结构模式。 Especially gems by design. 特别是宝石的设计。 You might not notice it at first if you are new to Ruby but once you get the hang of it; 如果您不熟悉Ruby,一开始可能不会注意到它,但是一旦掌握了它,您可能会发现它。 things are quite easy to navigate by the naming alone. 仅通过命名即可轻松浏览所有内容。

To the original intent of your question. 出于您问题的初衷。 I don't think there is an easy way to port a piece of Ruby code like SASS to python in a snap. 我认为没有一种简单的方法可以快速将SASS之类的Ruby代码移植到python。 It would require adequate knowledge of both languages to translate it well. 要很好地翻译这两种语言,将需要足够的知识。 In a way it's kind of like the job of a translator, if you don't know either language well you won't make a lot of sense by what you are producing. 从某种意义上说,这就像翻译员的工作,如果您不熟悉任何一种语言,那么您所制作的内容都不会很有意义。

In Ruby require loads a file and executes it. 在Ruby中,require加载文件并执行它。 A library will usually have a file named like the library that will include other parts of its components, to make the modules within known. 一个库通常会有一个像库一样的文件名,该文件将包括其组件的其他部分,以使其中的模块成为已知文件。

# req.rb
puts 'Hello World'

# irb
1.9.2p290 :001 > require './req.rb'
Hello World
=> true 

So it will basically do the same for you as import in python. 因此,基本上,它与python中的import一样。 You name a module you would like to use and it will work for you after requiring/importing it(given it is known to your load paths). 您可以为要使用的模块命名,并且在需要/导入模块后即可使用(如果您的加载路径已知该模块)。 If you are looking for an equivalent to the python 如果您正在寻找与python等价的产品

from <modulename> import *

you should refer to 你应该参考

include <modulename>

Which isn't frowned upon as much in ruby as it is in python. 红宝石并不像python那样被皱眉。

Hope this answers most of your questions. 希望这能回答您的大多数问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM