一个令人恼火的问题关于红宝石哈希 [英] An irritating Issue about ruby hashes
问题描述
例如,我有一个名为'required'的函数,它需要一个散列,所以我这样做:
required {base_uri:String,endpoints:String,destination:String,credentials:Hash}
,它认为它是
解决这个问题的唯一方法是在括号中加上括号,以便它成为:
required({base_uri:String,endpoints:String,destination:String,credentials:Hash})
然后开始读它作为一个散列(就像我想要的那样)。
我不打扰在哈希周围放置括号,什么令人讨厌是不一致的,有时是有效的,有时并不一致。
怎么回事? 在方法调用中,你必须在哈希周围省略花括号。
Why does ruby sometimes interpret a hash as being a block of code?
For example I have a function called 'required' that takes a hash so I do:
required { base_uri: String, endpoints: String, destination: String, credentials: Hash}
and it think's it's a block of code and errors with unexpected tSYMBEG
.
The only way to fix this is to put brackets around it so it reads as:
required({ base_uri: String, endpoints: String, destination: String, credentials: Hash})
It then starts reading it as a hash (like I want it to).
I'm not bothered about putting brackets around the hash, what's irritating is that it's inconsistent, sometimes that works and sometimes it doesn't.
What's going on?
If you omitted the parentheses of the method call, you have to omit curly braces around hashes too.
这篇关于一个令人恼火的问题关于红宝石哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!