[英]Rails Model Syntax Confusion
I came across this code in Rails app using mongodb: 我在mongodb的Rails应用程序中遇到了以下代码:
"""
Folder format:
{
name: <folder name>,
stocks: [
{
name: <stock name>,
id: <stock id>,
qty: <stock quantity>
}
]
}
"""
def format_with_folders(stocks)
fmap = stock_folder_map
res = stocks.group_by {|s| fmap[s["id"]] }.collect {|fname, ss|
{
"name" => fname,
"stocks" => ss
}
}
new(folders: res)
end
def stock_folder_map
res = {}
folders.each { |ff|
ff.stocks.each { |s|
res[s["id"]] = ff["name"]
}
}
return res
end
end
The doubts are: 1) What does the code inside triple quote signify? 令人怀疑的是:1)三引号内的代码表示什么? Is is a commented code?
是注释代码吗? 2)What would be the right format to use this code inside a ruby script?
2)在ruby脚本中使用此代码的正确格式是什么?
First of all, the triple quoted string is often used as a comment, and that is the case here. 首先,三引号字符串通常用作注释,在这里就是这种情况。
To get this to work outside of the class, you would need create a folders method that returns an array of folders in the correct structure. 为了使它在类之外起作用,您需要创建一个folders方法,该方法以正确的结构返回文件夹数组。 You could do something like this:
您可以执行以下操作:
Folder = Struct.new(:name, :stocks)
def folders
[
Folder.new(
"Folder 1",
[
{ "name" => "stock name", "id" => "stock id", "qty" => 3 },
{ "name" => "stock name", "id" => "stock id", "qty" => 5 }
]
),
Folder.new(
"Folder 2",
[
{ "name" => "stock name", "id" => "stock id", "qty" => 2 },
{ "name" => "stock name", "id" => "stock id", "qty" => 1 }
]
)
]
end
def format_with_folders(stocks)
# ...
end
def stock_folder_map
# ...
end
The folders method returns an array of Folder
objects, which both have a name
and stocks
attribute. Folders方法返回一个
Folder
对象数组,它们同时具有name
和stocks
属性。 Stocks are an array of hashes. 股票是一堆哈希。
In Ruby, if you have multiple string literals next to each other, they get concatenated at parse time: 在Ruby中,如果多个字符串文字彼此相邻,则它们在解析时会串联在一起:
'foo' "bar"
# => 'foobar'
This is a feature inspired by C. 这是受C启发的功能。
So, what you have there is three string literals next to each other. 因此,您拥有的三个字符串文字彼此相邻。 The first string literal is the empty string:
第一个字符串文字是空字符串:
""
Then comes another string literal: 然后是另一个字符串文字:
"
Folder format:
{
name: <folder name>,
stocks: [
{
name: <stock name>,
id: <stock id>,
qty: <stock quantity>
}
]
}
"
And lastly, there is a third string literal which is again empty: 最后,还有第三个字符串文字,它再次为空:
""
At parse time, this will be concatenated into a single string literal: 在解析时,它将被连接成一个字符串文字:
"
Folder format:
{
name: <folder name>,
stocks: [
{
name: <stock name>,
id: <stock id>,
qty: <stock quantity>
}
]
}
"
And since this string object isn't referenced by anything, isn't assigned to any variable, isn't returned from any method or block, it will just get immediately garbage collected. 而且由于此字符串对象未被任何内容引用,未被分配给任何变量,也不从任何方法或块返回,因此它将立即被垃圾回收。
In other words: the entire thing is a no-op, it's dead code. 换句话说:整个事情都是空话,它是无效的代码。 A sufficiently smart Ruby compiler (such as JRuby or Rubinius) will probably completely eliminate it, compile it into nothing.
一个足够聪明的Ruby编译器(例如JRuby或Rubinius)可能会完全消除它,将其编译为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.