一个令人恼火的问题关于红宝石哈希 [英] An irritating Issue about ruby hashes

查看:119
本文介绍了一个令人恼火的问题关于红宝石哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么红宝石有时会将散列解释为一段代码?



例如,我有一个名为'required'的函数,它需要一个散列,所以我这样做:

  required {base_uri:String,endpoints:String,destination:String,credentials:Hash} 

,它认为它是意外的tSYMBEG 的代码块和错误块。



解决这个问题的唯一方法是在括号中加上括号,以便它成为:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆