Closed elifoster closed 7 years ago
For example
def edit(title, text, opts = { bot: true } ) params = { title: title, text: text } params[:bot] = '1' if opts[:bot] params[:summary] ||= opts[:summary] params[:minor] = '1' if opts[:minor] params end # => :edit edit('title', 'text') # => {:title=>"title", :text=>"text", :bot=>"1", :summary=>nil} edit('title', 'text', summary: 'summary') # => {:title=>"title", :text=>"text", :summary=>"summary"} edit('title', 'text', summary: 'summary', minor: true) # => {:title=>"title", :text=>"text", :summary=>"summary", :minor=>"1"}
It happens because the hash argument is overwritten by the provided hash ({ summary: 'summary', minor: true }). Defaults should be specified like
{ summary: 'summary', minor: true }
def edit(title, text, opts = { } ) opts[:bot] = opts[:bot].nil? ? true : opts[:bot] params = { title: title, text: text } params[:bot] = '1' if opts[:bot] params[:summary] ||= opts[:summary] params[:minor] = '1' if opts[:minor] params end
instead, or something along those lines (it depends on the type of value).
For example
It happens because the hash argument is overwritten by the provided hash (
{ summary: 'summary', minor: true }
). Defaults should be specified likeinstead, or something along those lines (it depends on the type of value).