确定字符串是否为 Ruby 保留字的内置方法?

发布于 2024-11-17 07:26:13 字数 67 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

套路撩心 2024-11-24 07:26:14

使用 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 is string.example too.

再可℃爱ぅ一点好了 2024-11-24 07:26:14

如果你有一个类想要实现一个名为“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

instance.methods

This will show you all the methods inherited for the class, and if "protected" is there then it is reserved by ruby.

单身情人 2024-11-24 07:26:13

我能想到的唯一方法是加载一个包含您知道的所有关键字的数组。

class String
  def is_keyword?
    %w{__FILE__ __LINE__ alias and begin BEGIN break case class def defined? do else elsif end END ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield}.include? self
  end
end
"foobar".is_keyword? # => false
"for".is_keyword? # => true

供参考:

  • 知道这不是内置方式,它只是我能想到的唯一方式。不要因此对我投反对票。
  • 我包含的列表是真正的关键字列表。 publicprotected 和朋友并不是真正的关键字,只是在创建模块或类期间调用的重要方法的名称。您可以将其视为 Ruby DSL 的元素。

The only way I can think of is loading an array with all the keywords you know about.

class String
  def is_keyword?
    %w{__FILE__ __LINE__ alias and begin BEGIN break case class def defined? do else elsif end END ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield}.include? self
  end
end
"foobar".is_keyword? # => false
"for".is_keyword? # => true

For reference:

  • I know this isn't a built-in way, it's just the only way I could think of. Don't downvote me for it.
  • The list I included is that of true keywords. 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.
晚雾 2024-11-24 07:26:13

据我所知,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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文