简体   繁体   English

在ruby中生成JavaScript代码的最佳方法(RoR)

[英]Best way to generate javascript code in ruby (RoR)

I have seen some rails plugins which generate javascript code dynamically using ruby. 我已经看到一些Rails插件,它们使用ruby动态生成JavaScript代码。

1. 1。

%Q ( mixed block of javascript and ruby )

2. 2。

<<-CODE
some mixed ruby and javascript code
CODE

Being a java developer I don't understand 作为我不懂的Java开发人员

  1. what those strange looking syntax mean ? 这些看起来很奇怪的语法是什么意思?

  2. Is one way better than the other ? 一种方法比另一种更好吗?

  3. can anyone point me to proper documentation about such things ? 谁能指出我有关此类事情的适当文档?

The first syntax is Ruby's string literal syntax. 第一种语法是Ruby的字符串文字语法。 Specifically, the %Q (capital Q as opposed to lower-case) means that the string will be interpolated. 具体来说,%Q(大写Q而不是小写)表示将对字符串进行插值。 eg: 例如:

%Q[Here's a string with #{a_variable} interpolated!]

Note that you can use any arbitrary characters as the open and close delimiters. 请注意,您可以使用任何任意字符作为打开和关闭分隔符。

The second syntax is Ruby's heredoc syntax . 第二种语法是Ruby的Heredoc语法 The dash after the opening << indicates that Ruby will strip whitespace from the beginning of input lines contained in the heredoc block. 开头<<后面的破折号表示Ruby将从Heredoc块中包含的输入行的开头删除空格。


Ruby on Rails ships with the Prototype JavaScript framework built-in already. Ruby on Rails已经附带了Prototype JavaScript框架 It also ships with JS generator helper methods which generate the Prototype code dynamically based on Ruby code. 它还附带JS生成器帮助器方法 ,该方法基于Ruby代码动态生成Prototype代码。

You needn't use these if you don't want to. 如果不想,则不需要使用这些。 In fact, I rarely use them or Prototype at all, as jQuery is my JS framework of choice. 实际上,我很少使用它们或Prototype,因为jQuery是我选择的JS框架。 So one way is not "better" than the other (except in the general sense that heredoc is better than the string literal syntax for certain cases). 因此,一种方法不会比另一种“更好”(在一般情况下,heredoc在某些情况下比字符串文字语法更好)。

In Ruby %Q provides a double quote delimited string, so: 在Ruby中, %Q提供了双引号分隔的字符串,因此:

%Q(mixed block of javascript and ruby) #=> "mixed block of javascript and ruby"

<<-CODE is what Ruby calls a Here Document , or simply heredoc . <<-CODE是Ruby所谓的Here Document或简称heredoc This is a mechanism for creating free format strings whilst preserving special characters such as new lines and tabs. 这是一种创建自由格式字符串同时保留特殊字符(例如换行符和制表符)的机制。

A heredoc is created by preceding the text with << followed by the delimiter string you wish to use to mark the end of the text. 通过在文本之前添加<<以及您要用来标记文本结尾的定界字符串,可以创建一个Heredoc。

text = <<-DOC
To be, or not to be: that is the question

William Shakespeare
DOC

When this string is printed it appears exactly as it was entered, together with all the new lines and tabs: 打印此字符串时,它与输入的内容完全一样,以及所有新行和新标签:

To be, or not to be: that is the question

William Shakespeare
  1. %Q is the equivalent to a "" string in Ruby. %Q等效于Ruby中的""字符串。 But if you use such %Q-syntax, you don't need to escape double quotes. 但是,如果使用这种%Q语法,则无需转义双引号。
  2. It's a HEREDOC declaration. 这是HEREDOC声明。 You also don't need to escape quotes there. 您也不需要在此处转义报价。
  3. Strings in Ruby . Ruby中的字符串

Here you can find the details. 在这里您可以找到详细信息。 Ruby with javascript Ruby与JavaScript

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

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