铁轨上的红宝石,变量后面或前面的冒号

时间:2022-10-26 16:55:16

I am new to ruby , and rails both. I think Rails is one of the best API, and ruby is quite unique, it seems that ruby can cleverly do "assumptions" to help developer. But I am not too sure to what extend.

my questions is about colon in variable.
What i have understand so far is that :variable in ruby, is to say that this variable will not be able to change, which is similar to constant in other language. Am I correct??

我是ruby的新手,并且都是两个。我认为Rails是最好的API之一,而且ruby非常独特,似乎ruby可以巧妙地做“假设”来帮助开发人员。但我不太确定会延伸到什么程度。我的问题是变量中的冒号。到目前为止我所理解的是:ruby中的变量,就是说这个变量将无法改变,这与其他语言中的常量相似。我对么??

Then my confusion is, sometimes i see the colon infront of variable, like this

然后我的困惑是,有时我看到变量前面的冒号,就像这样

 Rails.application.config.session_store :cookie_store, 
      key: '_blog_session'
  <%= link_to "Delete", article, confirm: "Are you sure?", 
      method: :delete %>

Both key: and method: has colon in front.What does that this represent? and Furthermore

关键:和方法:前面有冒号。这代表什么?而且

Blog::Application.routes.draw.do
  root :to => "articles#index"
end

There are double colons inbetween variables? now I am guessing that Blog: is one variable, and :Application is constant. which i doubt it is, because it doesnt make sense. Please enlighten me?

thank you

变量之间有双冒号?现在我猜测Blog:是一个变量,而且:应用程序是不变的。我怀疑它是什么,因为它没有意义。请赐教?谢谢

2 个解决方案

#1


36  

What i have understand so far is that :variable in ruby, is to say that this variable will not be able to change, which is similar to constant in other language.

到目前为止我所理解的是:ruby中的变量,就是说这个变量将无法改变,这与其他语言中的常量相似。

I'm not sure if I understand that statement. In Ruby, constants start with an uppercase letter:

我不确定我是否理解这一说法。在Ruby中,常量以大写字母开头:

Foo = 1

Reassignment generates a warning:

重新分配会生成警告:

Foo = 1
Foo = 2 #=> warning: already initialized constant Foo

Variables start with a lowercase letter and reassignment doesn't cause a warning (they are supposed to change):

变量以小写字母开头,重新分配不会引发警告(它们应该更改):

foo = 1
foo = 2 # no warning

Symbols start with a colon:

符号以冒号开头:

:a_symbol
:Uppercase_symbol
:"i'm a symbol, too"

They often represent static values, e.g. :get and :post. Symbols are memory efficient, because they are created only once - the same symbol literal always returns the same object. Checking if two symbols are equal is a cheap operation.

它们通常表示静态值,例如:get和:post。符号是内存有效的,因为它们只创建一次 - 相同的符号文字总是返回相同的对象。检查两个符号是否相等是一种廉价的操作。

Both key: and method: (...) What does that this represent?

关键:和方法:( ...)这代表什么?

This is an alternate syntax for hashes. You can type it in IRB to see the result:

这是哈希的替代语法。您可以在IRB中键入它以查看结果:

{ foo: 1, bar: 2 }
#=> {:foo=>1, :bar=>2}

There are double colons inbetween variables? now I am guessing that Blog: is one variable, and :Application is constant.

变量之间有双冒号?现在我猜测Blog:是一个变量,而且:应用程序是不变的。

No, Blog and Application are both constants and :: is the scope resolution operator. It can be used to access nested constants, e.g.:

不,博客和应用程序都是常量,::是范围解析运算符。它可用于访问嵌套常量,例如:

module Foo
  class Bar
    BAZ = 123
  end
end

Foo::Bar::BAZ #=> 123

#2


3  

Rails.application.config.session_store :cookie_store, key: '_blog_session'

session_store is a method that takes two "Arguments":

session_store是一个带有两个“Arguments”的方法:

  • :cookie_store is a Symbol
  • :cookie_store是一个符号
  • key: '_blog_session' is actually a short way of writing a Hash.
  • key:'_ blog_session'实际上是编写Hash的简短方法。

(could also be session_store :cookie_store, { key: '_blog_session' })

(也可以是session_store:cookie_store,{key:'_ blog_session'})

Similarly for link_to "Delete", article, confirm: "Are you sure?", method: :delete

同样对于link_to“删除”,文章,确认:“你确定吗?”,方法:: delete

  • "Delete" is a string
  • “删除”是一个字符串
  • article a variable
  • 文章变量
  • { confirm: '...', method: :delete } hash where confirm:, method: and :delete are Symbols again.
  • {confirm:'...',方法:: delete} hash确认:,方法:和:删除符号再次。

While Blog::Application :: is basically a namespace resolution operator. A way for you to address the Application class in the Blog module.

而Blog :: Application ::基本上是命名空间解析运算符。一种在Blog模块中处理Application类的方法。

Hope this helps. Have a look at the documentation I referenced, it is explained rather nicely.

希望这可以帮助。看看我引用的文档,它的解释相当不错。

#1


36  

What i have understand so far is that :variable in ruby, is to say that this variable will not be able to change, which is similar to constant in other language.

到目前为止我所理解的是:ruby中的变量,就是说这个变量将无法改变,这与其他语言中的常量相似。

I'm not sure if I understand that statement. In Ruby, constants start with an uppercase letter:

我不确定我是否理解这一说法。在Ruby中,常量以大写字母开头:

Foo = 1

Reassignment generates a warning:

重新分配会生成警告:

Foo = 1
Foo = 2 #=> warning: already initialized constant Foo

Variables start with a lowercase letter and reassignment doesn't cause a warning (they are supposed to change):

变量以小写字母开头,重新分配不会引发警告(它们应该更改):

foo = 1
foo = 2 # no warning

Symbols start with a colon:

符号以冒号开头:

:a_symbol
:Uppercase_symbol
:"i'm a symbol, too"

They often represent static values, e.g. :get and :post. Symbols are memory efficient, because they are created only once - the same symbol literal always returns the same object. Checking if two symbols are equal is a cheap operation.

它们通常表示静态值,例如:get和:post。符号是内存有效的,因为它们只创建一次 - 相同的符号文字总是返回相同的对象。检查两个符号是否相等是一种廉价的操作。

Both key: and method: (...) What does that this represent?

关键:和方法:( ...)这代表什么?

This is an alternate syntax for hashes. You can type it in IRB to see the result:

这是哈希的替代语法。您可以在IRB中键入它以查看结果:

{ foo: 1, bar: 2 }
#=> {:foo=>1, :bar=>2}

There are double colons inbetween variables? now I am guessing that Blog: is one variable, and :Application is constant.

变量之间有双冒号?现在我猜测Blog:是一个变量,而且:应用程序是不变的。

No, Blog and Application are both constants and :: is the scope resolution operator. It can be used to access nested constants, e.g.:

不,博客和应用程序都是常量,::是范围解析运算符。它可用于访问嵌套常量,例如:

module Foo
  class Bar
    BAZ = 123
  end
end

Foo::Bar::BAZ #=> 123

#2


3  

Rails.application.config.session_store :cookie_store, key: '_blog_session'

session_store is a method that takes two "Arguments":

session_store是一个带有两个“Arguments”的方法:

  • :cookie_store is a Symbol
  • :cookie_store是一个符号
  • key: '_blog_session' is actually a short way of writing a Hash.
  • key:'_ blog_session'实际上是编写Hash的简短方法。

(could also be session_store :cookie_store, { key: '_blog_session' })

(也可以是session_store:cookie_store,{key:'_ blog_session'})

Similarly for link_to "Delete", article, confirm: "Are you sure?", method: :delete

同样对于link_to“删除”,文章,确认:“你确定吗?”,方法:: delete

  • "Delete" is a string
  • “删除”是一个字符串
  • article a variable
  • 文章变量
  • { confirm: '...', method: :delete } hash where confirm:, method: and :delete are Symbols again.
  • {confirm:'...',方法:: delete} hash确认:,方法:和:删除符号再次。

While Blog::Application :: is basically a namespace resolution operator. A way for you to address the Application class in the Blog module.

而Blog :: Application ::基本上是命名空间解析运算符。一种在Blog模块中处理Application类的方法。

Hope this helps. Have a look at the documentation I referenced, it is explained rather nicely.

希望这可以帮助。看看我引用的文档,它的解释相当不错。