Ruby to Groovy

3 ruby groovy

我有一个用Ruby编写的框架,需要转换为Groovy.
它不使用核心ruby之外的任何东西,而是使用很多元编程.

Groovy是否支持所有相同的基本功能,并且转换是否复杂?

phs*_*hss 8

Groovy和Ruby并没有完全不同,但元编程方面有所改变.

虽然我不是Groovy专家,但我可以在文档中引用一些指针(http://groovy.codehaus.org/Dynamic+Groovy):

动态方法调用:

# Ruby
an_instance.send("method_name")

// Groovy
anInstance."$methodName"()
Run Code Online (Sandbox Code Playgroud)

缺少方法:

# Ruby
def method_missing(meth, *args, &blk)
  # Some code
end

// Groovy
def methodMissing(String name, args) {
  // Some code
}
Run Code Online (Sandbox Code Playgroud)

在运行时向类添加方法:

# Ruby
class SomeObject
  define_method :new_method do
    # Do something
  end
end

// Groovy
SomeObject.metaClass.newMethod = {->
  // Do something
}
Run Code Online (Sandbox Code Playgroud)


Bri*_*new 5

我怀疑它可能并不容易(取决于大小,功能等),并不是翻译作为重写.每当我发现自己想要进行重写时,我会先谈谈Joel对此的思考,然后再进一步讨论.

你为什么需要在Groovy中重做这个?如果你需要JVM(比如,整合其他库/框架),你看过JRuby吗?它可能会为您节省大量的工作和痛苦.