嗯...这是代码
func howMany() -> Int {
return 10
}
func Call() -> Void {
guard case let output = howMany(), output > 5 else { return }
}
Call()
我真的不明白保护套是如何工作的。这看起来很像一个模式匹配条件,我们比较 howMany() 的结果是否等于 output,如果是,则将值赋给 output 然后将其与文字值 5 进行比较。但是,当我删除 output > 5 行时,编译器说,“保护条件始终为真,正文不可访问。”
按照pattern,如果我们把它翻译成switch语句,大概是这样
switch howMany() {
case let output where output > 5:
break;
}
问题是如果可以直接翻译成switch语句,那么我们删除where条件时,应该不会出现“守卫条件始终为真,body不可达”的警告.
我希望有人能对此有所启发。
最佳答案
考虑:
func foo() {
guard case let output = howMany(), output > 5 else {
print("less than or equal to 5")
return
}
print("\(output) is greater than 5")
}
这大致相当于:
func bar() {
switch howMany() {
case let output where output > 5:
print("\(output) is greater than 5")
default:
print("less than or equal to 5")
return
}
}
如果删除 > 5 条件:
func foo() {
guard case let output = howMany() else {
print("less than or equal to 5")
return
}
print("\(output) is greater than 5")
}
您收到警告:
'guard' condition is always true, body is unreachable
这个警告是正确的,因为 body 是不可到达的。
如果您在 switch 示例中执行等效操作:
func bar() {
switch howMany() {
case let output:
print("\(output) is greater than 5")
default:
print("less than or equal to 5")
return
}
}
如果这样做,您将收到类似的警告:
default will never be executed
而且,这又是有道理的,因为 default 不会达到。
现在,考虑您的示例 switch 没有 default 子句:
func bar() {
switch howMany() {
case let output:
print("output:", output)
}
}
你在这里没有收到警告只是因为没有 default 子句(类似于 guard 语句的“body”)。
关于swift - guard 案件分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53821290/
通过rubykoans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
假设您在Ruby中执行此操作:ar=[1,2]x,y=ar然后,x==1和y==2。是否有一种方法可以在我自己的类中定义,从而产生相同的效果?例如rb=AllYourCode.newx,y=rb到目前为止,对于这样的赋值,我所能做的就是使x==rb和y=nil。Python有这样一个特性:>>>classFoo:...def__iter__(self):...returniter([1,2])...>>>x,y=Foo()>>>x1>>>y2 最佳答案 是的。定义#to_ary。这将使您的对象被视为要分配的数组。irb>o=Obje
我正在使用Dragonfly在Rails3.1应用程序上处理图像。我正在努力通过url将图像分配给模型。我有一个很好的表格:{:multipart=>true}do|f|%>RemovePicture?Dragonfly的文档指出:Dragonfly提供了一个直接从url分配的访问器:@album.cover_image_url='http://some.url/file.jpg'但是当我在控制台中尝试时:=>#ruby-1.9.2-p290>picture.image_url="http://i.imgur.com/QQiMz.jpg"=>"http://i.imgur.com/QQ
使用Paperclip,我想从这样的URL抓取图像:require'open-uri'user.photo=open(url)问题是我最后得到一个像“open-uri20110915-4852-1o7k5uw”这样的文件名。有什么方法可以更改user.photo上的文件名?作为一个额外的变化,Paperclip将我的文件存储在S3上,所以如果我可以在初始分配中设置我想要的文件名就更好了,这样图像就会上传到正确的S3key。像这样:user.photo=open(url),:filename=>URI.parse(url).path 最佳答案
这是我理想中想要的。用户做:a="hello"输出为Youjustallocated"a"!=>"Hello"顺序无关紧要,只要我能实现该消息即可。 最佳答案 不,没有直接的方法可以做到这一点,因为在执行代码之前,Ruby字节码编译器会丢弃局部变量名。YARV(MRI1.9.2中使用的RubyVM)提供的关于局部变量的唯一指令是getlocal和setlocal,它们都对整数索引进行操作,而不是变量名。以下是1.9.2源代码中insns.def的摘录:/****************************************
我刚刚更新了我的gem,当我尝试运行Guard时,出现以下错误:Guard::RSpecDEPRECATIONWARNING:The:clioptionisdeprecated.Pleasecustomizethenew:cmdoptiontofityourneed.这是我的Guard文件:guard'rspec',cli:'--drb'dowatch(%r{^spec/.+_spec\.rb$})watch(%r{^lib/(.+)\.rb$}){|m|"spec/lib/#{m[1]}_spec.rb"}watch('spec/spec_helper.rb'){"spec"}#Ra
设置一个临时变量来交换数组中的两个元素似乎比使用并行赋值更有效。谁能帮忙解释下?require"benchmark"Benchmark.bmdo|b|b.reportdo40000000.times{array[1],array[2]=array[2],array[1]}endendBenchmark.bmdo|b|b.reportdo40000000.timesdot=array[1]array[1]=array[2]array[2]=tendendend结果:usersystemtotalreal4.4700000.0200004.490000(4.510368)usersyste
我有一个简单的类,它在初始化时接受一到八个参数。它将访问器设置为这些访问器以供以后使用。Rubocop正试图以ABC太高为由逮捕我,但我不确定我所做的是否真的有任何问题。在这种情况下,我只是在初始化时禁用检查吗?classFooattr_accessor:one,:two,:three,:fourattr_accessor:five,:six,:seven,:eightdefinitialize(p={})@one=p[:one]ifp[:one].present?#...@eight=p[:eight]ifp[:eight].present?endend关于减小大小,我唯一的想法是做
我正在学习Codecademy的Ruby类(class),大约完成了85%。它一遍又一遍地要求你创建一个类并传入一些参数并使它们成为实例变量,例如:classComputerdefinitialize(username,password)@username=username@password=passwordendend每次,它都会要求您制作与您传入的参数完全相同的实例变量。这让我想知道是否有一种Ruby方法可以自动处理这个问题,无需每次都自己输入。我知道你可以做到classComputerdefinitialize(username,password)@username,@passw