Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在Ruby中模拟Java类注释?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Ruby中模拟Java类注释?

(我们有答案,概括一下
http://bens.me.uk/2009/java-style-annotations-in-ruby)

@H_801_5@

解决方法

这几个星期前改编自 a piece of code I wrote in an answer to another question,然它当然不是原创的.毕竟,这是众所周知的Ruby惯用语,多年来一直在使用,至少从耙子的描述法开始. @H_937_9@module Annotations def Annotations(meth=nil) return @__Annotations__[meth] if meth @__Annotations__ end private def method_added(m) (@__Annotations__ ||= {})[m] = @__last_Annotation__ if @__last_Annotation__ @__last_Annotation__ = nil super end def method_missing(meth,*args) return super unless /\A_/ =~ meth @__last_Annotation__ ||= {} @__last_Annotation__[meth[1..-1].to_sym] = args.size == 1 ? args.first : args end end class Module private def Annotate! extend Annotations end end

这里有一个小例子:

class A
  Annotate!

  _Hello   color: 'red',ancho:   23
  _goodbye color: 'green',alto:  -123
  _foobar  color: 'blew'
  def m1; end

  def m2; end

  _foobar  color: 'cyan'
  def m3; end
end

当然,没有一个testuite,Ruby代码将不会完成:

require 'test/unit'
class TestAnnotations < Test::Unit::TESTCase
  def test_that_m1_is_Annotated_with_Hello_and_has_value_red
    assert_equal 'red',A.Annotations(:m1)[:Hello][:color]
  end
  def test_that_m3_is_Annotated_with_foobar_and_has_value_cyan
    assert_equal 'cyan',A.Annotations[:m3][:foobar][:color]
  end
  def test_that_m1_is_Annotated_with_goodbye
    assert A.Annotations[:m1][:goodbye]
  end
  def test_that_all_Annotations_are_there
    Annotations = {
      m1: {
        Hello:   { color: 'red',ancho:   23 },goodbye: { color: 'green',alto:  -123 },foobar:  { color: 'blew'               }
      },m3: {
        foobar:  { color: 'cyan'               }
      }
    }
    assert_equal Annotations,A.Annotations
  end
end
@H_801_5@ @H_801_5@

大佬总结

以上是大佬教程为你收集整理的如何在Ruby中模拟Java类注释?全部内容,希望文章能够帮你解决如何在Ruby中模拟Java类注释?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。