方法调用的参数中的冒号- Ruby

时间:2022-04-27 23:17:35

when looking to some Ruby code I found the following method:

在查找一些Ruby代码时,我发现了以下方法:

def connection
    unless @_mc_connection && valid? && @_ns_version == get_version
      @_mc_connection = ::Dalli::Client.new(self.dalli_servers, self.dalli_options.merge(namespace: namespace))
    end
    @_mc_connection
  end

My question is about the use of dalli_options.merge(namespace: namespace). What is the purpose of the colon here? Is an hash member?

我的问题是关于dalli_options的使用。合并(名称空间:名称空间)。冒号的作用是什么?是一个散列会员?

3 个解决方案

#1


5  

What is the purpose of the colon here? Is an hash member?

冒号的作用是什么?是一个散列会员?

Yes, it is a Hash object.

是的,它是一个散列对象。

A Hash can be easily created by using its implicit form:

可以使用它的隐式形式轻松创建散列:

grades = { "Jane Doe" => 10, "Jim Doe" => 6 }

Hashes allow an alternate syntax form when your keys are always symbols. Instead of

当键始终是符号时,散列允许使用另一种语法形式。而不是

options = { :font_size => 10, :font_family => "Arial" }

You could write it as:

你可以这样写:

options = { font_size: 10, font_family: "Arial" }

#2


1  

The colon is part of the symbol syntax.

冒号是符号语法的一部分。

The following are equivalent:

以下是等价的:

namespace:   #only valid inside a hash

and

:namespace

With the former, the 'hash rocket' operator (=>) can be omitted (and usually is, for ease of reading).

对于前者,可以省略'hash rocket'操作符(=>)(为了便于阅读,通常是这样)。

However, this is only the case when your keys are symbols. If your keys are strings, as in

然而,只有当你的键是符号时才会出现这种情况。如果你的键是字符串,比如。

{ 'namespace' => 'api' }

the hash rocket is required.

哈希火箭是必需的。

#3


1  

Depending on the Ruby version, this is either a Hash literal (1.9) or a keyword argument (2.0+).

根据Ruby版本的不同,这要么是散列文字(1.9),要么是关键字参数(2.0+)。

#1


5  

What is the purpose of the colon here? Is an hash member?

冒号的作用是什么?是一个散列会员?

Yes, it is a Hash object.

是的,它是一个散列对象。

A Hash can be easily created by using its implicit form:

可以使用它的隐式形式轻松创建散列:

grades = { "Jane Doe" => 10, "Jim Doe" => 6 }

Hashes allow an alternate syntax form when your keys are always symbols. Instead of

当键始终是符号时,散列允许使用另一种语法形式。而不是

options = { :font_size => 10, :font_family => "Arial" }

You could write it as:

你可以这样写:

options = { font_size: 10, font_family: "Arial" }

#2


1  

The colon is part of the symbol syntax.

冒号是符号语法的一部分。

The following are equivalent:

以下是等价的:

namespace:   #only valid inside a hash

and

:namespace

With the former, the 'hash rocket' operator (=>) can be omitted (and usually is, for ease of reading).

对于前者,可以省略'hash rocket'操作符(=>)(为了便于阅读,通常是这样)。

However, this is only the case when your keys are symbols. If your keys are strings, as in

然而,只有当你的键是符号时才会出现这种情况。如果你的键是字符串,比如。

{ 'namespace' => 'api' }

the hash rocket is required.

哈希火箭是必需的。

#3


1  

Depending on the Ruby version, this is either a Hash literal (1.9) or a keyword argument (2.0+).

根据Ruby版本的不同,这要么是散列文字(1.9),要么是关键字参数(2.0+)。