发布于 2016-01-26 02:53:44 | 131 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Ruby教程,程序狗速度看过来!

Ruby编程语言

Ruby,一种为简单快捷的面向对象编程(面向对象程序设计)而创的脚本语言,在20世纪90年代由日本人松本行弘开发,遵守GPL协议和Ruby License。它的灵感与特性来自于 Perl、Smalltalk、Eiffel、Ada以及 Lisp 语言。


这篇文章主要介绍了Ruby元编程的一些值得注意的地方,作者自己用实际代码给出了相关示例,需要的朋友可以参考下

  避免无限循环的元编程。

    写一个函数库时不要使核心类混乱(不要使用 monkey patch)。

    代码块形式最好用于字符串插值形式。
        当你使用字符串插值形式,总是提供 __FILE__ 和 __LINE__,使得你的回溯有意义。


 class_eval 'def use_relative_model_naming?; true; end', __FILE__, __LINE__

        define_method 最好用 class_eval{ def ... }

    当使用 class_eval (或者其他的 eval)以及字符串插值,添加一个注释块使之在插入的时候显示(这是我从 rails 代码学来的实践):


 # from activesupport/lib/active_support/core_ext/string/output_safety.rb
 UNSAFE_STRING_METHODS.each do |unsafe_method|
  if 'String'.respond_to?(unsafe_method)
  class_eval <<-EOT, __FILE__, __LINE__ + 1
   def #{unsafe_method}(*args, &block)  # def capitalize(*args, &block)
   to_str.#{unsafe_method}(*args, &block) # to_str.capitalize(*args, &block)
   end          # end

   def #{unsafe_method}!(*args)    # def capitalize!(*args)
   @dirty = true       # @dirty = true
   super         # super
   end          # end
  EOT
  end
 end

    避免在元编程中使用 method_missing,它使得回溯变得很麻烦,这个习惯不被列在 #methods,拼写错误的方法可能也在默默的工作,例如 nukes.launch_state = false。考虑使用委托,代理或者是 define_method ,如果必须这样,使用 method_missing ,
        确保 也定义了 respond_to_missing?
        仅捕捉字首定义良好的方法,像是 find_by_* ― 让你的代码越肯定(assertive)越好。
        在语句的最后调用 super
        delegate 到确定的、非魔法方法中:


 # bad
 def method_missing?(meth, *args, &block)
  if /^find_by_(?<prop>.*)/ =~ meth
  # ... lots of code to do a find_by
  else
  super
  end
 end

 # good
 def method_missing?(meth, *args, &block)
  if /^find_by_(?<prop>.*)/ =~ meth
  find_by(prop, *args, &block)
  else
  super
  end
 end

 # best of all, though, would to define_method as each findable attribute is declared




最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务