我想测试是否传递了关键字参数。
def test(:name, :zip)
if name.key.present?
#then do stuff
end
end
我不想只检查nil,而是因为是否有意通过nil
test(name: nil, zip: 10011)
,我希望该方法以不同的方式处理哈希,然后再从不传递关键字对。test(zip: 10011)
答案 0 :(得分:4)
您的代码不起作用,关键字参数定义为key: defaultvalue
您可以将“不可能”值定义为默认值并进行检查。 “不可能”表示永远不会用作参数的值。
def test(name: :just_some_stupd_content, zip: nil)
if name == :just_some_stupd_content
puts 'Name is not set'
else
puts 'Name is set'
#then do stuff
end
end
test(name: nil, zip: 10011)
test(zip: 10011)
使用以下命令调用时,此逻辑将不起作用
test(name: :just_some_stupd_content, zip: 10011)
所以它不是防弹的,但我认为这是一种有用的方法。
Ruby 2.1引入了必需的关键字参数,这些关键字参数以冒号结尾:
def test(name:, zip: nil)
puts 'Name is set'
end
test(name: nil, zip: 10011)
test(zip: 10011) #raises missing keyword: name (ArgumentError)
如果根据您的需要,此解决方案是否更合适。
答案 1 :(得分:2)
这里有些错误。
首先,您声明的方法不正确。请勿在参数名称中使用符号。如果我尝试通过RSpec以参数名称作为符号来运行方法定义,则会得到:
public class AccountDbContext : DbContext
{
private DbSet<AccountDataModel> TAccount { get; set; }
private DbSet<AccountCount> AccountList { get; set; }
public AccountDbContext(DbContextOptions<AccountDbContext> options) : base(options)
{
}
public Account GetAccountById(long accountId)
{
var pAccount = new SqlParameter("accountId", accountId);
Account account = TAccount.FromSql("EXEC dbo.SPR_Account_GetById @accountId", pAccount).FirstOrDefault();
return account;
}
public List<Account> GetAccounts(int offset, int limit)
{
var pOffset = new SqlParameter("offset", offset);
var pLimit = new SqlParameter("limit", limit);
var dataSet = AccountList.FromSql($"EXEC dbo.SPR_Account_GetAll @offset=@offset,@limit=@limit", pOffset, pLimit).ToList();
return dataSet;
}
}
只需:
12:20:27 - INFO - Running: spec/so/optional_args_spec.rb
An error occurred while loading ./spec/so/optional_args_spec.rb.
Failure/Error: __send__(method, file)
SyntaxError:
[REDACTED]/optional_args_spec.rb:5: syntax error, unexpected tSYMBEG, expecting ')'
def test(:name, :zip)
^
[REDACTED]/optional_args_spec.rb:24: syntax error, unexpected end-of-input, expecting keyword_end
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/configuration.rb:2053:in `load'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/configuration.rb:2053:in `load_file_handling_errors'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/configuration.rb:1560:in `block in load_spec_files'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/configuration.rb:1558:in `each'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/configuration.rb:1558:in `load_spec_files'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/runner.rb:98:in `setup'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/runner.rb:86:in `run'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/runner.rb:71:in `run'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/lib/rspec/core/runner.rb:45:in `invoke'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/gems/rspec-core-3.8.2/exe/rspec:4:in `<top (required)>'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/bin/rspec:23:in `load'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/bin/rspec:23:in `<main>'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/bin/ruby_executable_hooks:24:in `eval'
# [REDACTED]/.rvm/gems/ruby-2.5.1@so/bin/ruby_executable_hooks:24:in `<main>'
#
# Showing full backtrace because every line was filtered out.
# See docs for RSpec::Configuration#backtrace_exclusion_patterns and
# RSpec::Configuration#backtrace_inclusion_patterns for more information.
No examples found.
Finished in 0.00027 seconds (files took 0.42065 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
第二,如果您想这样做:
def test(name, zip)
end
由于指定了两个参数(test(name: nil, zip: 10011)
和name
),并且仅传递一个参数(哈希值:zip
),因此会出现错误。所以,做:
{name: nil, zip: 10011}
现在您可以这样做:
def test(args)
end