关于 Ruby 的一般问题

发布于 2024-07-14 14:06:11 字数 524 浏览 10 评论 0 原文

我已经在我的 Rails 应用程序中安装了来自 github.com 的acts_as_versioned 插件,并且有一个我不完全理解的代码块,我希望有人能为我解决这个问题

class_eval <<-CLASS_METHODS
  def a_bunch_of_stuff
   ....
  end
CLASS_METHODS

我知道该块内的方法(或不管它是什么)被定义为类内部的实例方法,但我在插件中的任何地方都找不到定义为常量的 CLASS_METHODS,而且我也不确定 class_eval 之后的 <<- 意味着什么。 该插件位于 此处,相关代码从 lib/ 的第 199 行开始acts_as_versioned.rb。 如果有人愿意告诉我这里的内幕,我将非常感激。

谢谢

-C

I have installed the acts_as_versioned plugin from github.com in my rails application, and there was a block of code that I don't fully understand, I was hoping someone could clear this up for me

class_eval <<-CLASS_METHODS
  def a_bunch_of_stuff
   ....
  end
CLASS_METHODS

I get that the methods inside the block (or whatever it is) gets defined as instance methods inside the class, but I can't find CLASS_METHODS defined as a constant anywhere in the plugin, and I'm also not sure what <<- after class_eval means. the plugin is located here, and the code in question starts on line 199 of lib/acts_as_versioned.rb. If someone would give me the lowdown here, I would be much-obliged.

thx

-C

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星星的轨迹 2024-07-21 14:06:11

这是一个异端邪说。 http://en.wikipedia.org/wiki/Heredoc#Ruby

匹配的 CLASS_METHODS 标记本质上是开始和结束引号。 如果使用 <<- 而不是 <<,则结束标记可以用空格缩进。

您可以在 Ruby 中同时使用多个heredocs(我使我的heredocs与参数名称相同,但这只是为了美观 - 它没有区别):

def define_with_description description, code
  puts "defining a method to #{description}"
  class_eval code
end

define_with_description <<-DESCRIPTION, <<-CODE
  set up us the bomb
DESCRIPTION
  Bomb.new.set_up(us)
CODE

It's a heredoc. http://en.wikipedia.org/wiki/Heredoc#Ruby

The matched CLASS_METHODS tokens are essentially starting and ending quotes. If you use <<- instead of <<, the ending token can be indented with whitespace.

You can use multiple heredocs at once in Ruby (I made my heredocs the same as the argument names, but that's just for aesthetic - it makes no difference):

def define_with_description description, code
  puts "defining a method to #{description}"
  class_eval code
end

define_with_description <<-DESCRIPTION, <<-CODE
  set up us the bomb
DESCRIPTION
  Bomb.new.set_up(us)
CODE
∞琼窗梦回ˉ 2024-07-21 14:06:11

这是一个“此处文档”,这是一种内联引用大型多行字符串的方法。 来自 Ruby 编程

字符串可以跨多个输入行继续,在这种情况下它们将包含换行符。 也可以使用此处文档来表达长字符串文字。 每当 Ruby 解析序列 << 时,它都会将其替换为从连续逻辑输入行构建的字符串文字。 当它找到以标识符或带引号的字符串开头的行时,它会停止构建字符串。 您可以在 << 字符后面放置一个减号,在这种情况下,终止符可以从左边距缩进。 如果使用带引号的字符串来指定终止符,则其引用规则将应用于此处文档; 否则,适用双引号规则。


这将导致 class_evalCLASS_METHODS 之间的内容视为字符串,并在适当的上下文中执行它。 名称CLASS_METHODS的使用是任意的,它可以很容易地被称为其他名称。

This is a "here document", which is a way of quoting large multiline strings inline. From Programming Ruby:

Strings can continue across multiple input lines, in which case they will contain newline characters. It is also possible to use here documents to express long string literals. Whenever Ruby parses the sequence <<identifier or <<quoted string, it replaces it with a string literal built from successive logical input lines. It stops building the string when it finds a line that starts with the identifier or the quoted string. You can put a minus sign immediately after the << characters, in which case the terminator can be indented from the left margin. If a quoted string was used to specify the terminator, its quoting rules will be applied to the here document; otherwise, double-quoting rules apply.

This will cause class_eval to treat the stuff between CLASS_METHODS as a string and execute it in the appropriate context. The use of the name CLASS_METHODS is arbitrary, it could just as easily have been called anything else.

不再见 2024-07-21 14:06:11

这些是等效的:

class SomeClass
  class_eval <<-CLASS_METHODS
    def first_method
    end
    def second_method
    end
  CLASS_METHODS
end

class SomeClass
  def self.first_method
  end
  def self.second_method
  end
end

These are equivalent:

class SomeClass
  class_eval <<-CLASS_METHODS
    def first_method
    end
    def second_method
    end
  CLASS_METHODS
end

class SomeClass
  def self.first_method
  end
  def self.second_method
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文