确定字符串是否为 Ruby 保留字的内置方法?
Ruby 是否有内置的东西来确定字符串是否是保留字?类似“next”.is_keyword?
?
Is there something built-in with Ruby to determine if a string is a reserved word? Something like "next".is_keyword?
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Ruby gem rubykeyword。它的作用不仅仅是识别关键字。
string.keyword?
告诉您它是否是关键字。string.define
给出关键字的定义。还有string.example
。Use Ruby gem rubykeyword. It does more than identifying the keyword.
string.keyword?
tells you if its keyword or not.string.define
gives a definition of the keyword. There isstring.example
too.如果你有一个类想要实现一个名为“protected”的方法,那么在定义该方法之前创建该类的实例并调用
这将显示该类继承的所有方法,如果存在“protected”,那么它是由 ruby 保留的。
If you have a class where you want to implement a method called "protected", then before you define that method make an instance of that class and call
This will show you all the methods inherited for the class, and if "protected" is there then it is reserved by ruby.
我能想到的唯一方法是加载一个包含您知道的所有关键字的数组。
供参考:
public
、protected
和朋友并不是真正的关键字,只是在创建模块或类期间调用的重要方法的名称。您可以将其视为 Ruby DSL 的元素。The only way I can think of is loading an array with all the keywords you know about.
For reference:
public
,protected
and friends aren't really keywords, just the names of important methods which are called during the creation of modules or classes. You can think of it as elements of the Ruby DSL.据我所知,
protected
并不是真正的保留字。这只是一个重要方法的名称。http://wiki.rubyonrails.org/rails/pages/reservedwords 列出了您的保留字不能使用(其中一些仅在您使用 Rails 或其依赖项时才适用),以及可能导致问题的方法名称。
如果这不能完全回答您的问题,您能否更全面地定义您是否对方法名称或变量名称感兴趣,以及您是否担心根本无法使用的单词,或者可能导致其他问题的单词事情出问题了吗?
As far as I know,
protected
isn't really a reserved word. It's just the name of an important method.http://wiki.rubyonrails.org/rails/pages/reservedwords lists reserved words you can't use (some of them only apply if you're using Rails or its dependencies), and method names that can cause problems.
If this doesn't fully answer your question, can you define more fully whether you're interested in method names or variable names, and whether you're worried about words that can't be used at all, or words that may cause other things to go wrong?