简体   繁体   English

尾随冒号在Rake任务名称中的含义是什么?

[英]What does a trailing colon mean in the name of a Rake task?

In the custom Rake task example: 在自定义Rake任务示例中:

desc "I am short, but comprehensive description for my cool task"
task task_name: [:prerequisite_task, :another_task_we_depend_on] do
  # All your magic here
  # Any valid Ruby code is allowed
end

What does the colon suffix after task_name indicate? task_name后的冒号后缀是什么意思? Normally, the task name is a symbol - what's different about this if you have prerequisites? 通常情况下,任务名称是一个符号 - 如果您有先决条件,这有什么不同?

The literal {task_name: [:symbol]} is a shorthand notation for {:task_name => [:symbol]} , this notation was introduced with Ruby 1.9: 文字{task_name: [:symbol]}{:task_name => [:symbol]}的简写符号,这个符号是用Ruby 1.9引入的:

{task_name: [:symbol]}
# => {:task_name=>[:symbol]}

Your example is actually equivalent to: 您的示例实际上相当于:

task :task_name => [:prerequisite_task, :another_task_we_depend_on] do
  ...

If you have no prerequisites you have to use the usual symbol notaion: 如果您没有先决条件,则必须使用通常的符号notaion:

task :task_name do
  ...

The Ruby Style Guide suggests to prefer the newer, "JSON style", notation. Ruby样式指南建议更喜欢较新的“JSON样式”符号。

Syntax something: value means exactly same as :something => value . 语法something: value意味着完全相同:something => value It is just a little more readable. 它只是更具可读性。

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

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