Ruby on Rails:"等于"符号的含义是什么?

hap*_*sad 8 ruby parameters ruby-on-rails equals

我一直在使用的一些开源有以下行作为函数声明:

def parse_query(query = nil,options = {},models = nil)

"等于"符号对声明有什么影响?它只是使参数可选吗?

Oli*_* N. 13

如果调用该函数的人没有指定参数,它会设置参数的默认值.


Dan*_*enc 5

与Python和C++类似,参数列表中的等号允许您指定默认参数.例如,在Python中:

def hello_world(message="Hello World"):
    print "message = "+message
Run Code Online (Sandbox Code Playgroud)

像这样调用这个函数:

hello_world()
Run Code Online (Sandbox Code Playgroud)

将导致:

message = Hello World
Run Code Online (Sandbox Code Playgroud)

但是调用这样的函数:

hello_world("changed default")
Run Code Online (Sandbox Code Playgroud)

结果是:

message = changed default
Run Code Online (Sandbox Code Playgroud)