【问题标题】:What's the maximum length of a method name in ruby?ruby 中方法名称的最大长度是多少?
【发布时间】:2011-12-17 15:55:30
【问题描述】:

我正在使用 RubyMine,它告诉我

unsubscribe_from_all_notifications

...对于方法名称来说太长了。最大长度是多少?

【问题讨论】:

  • 尝试逐渐减少这个方法名称的长度,看看它在什么时候接受它。 ;)
  • 我猜这是 RubyMine 的问题;我尝试了一个名为 unsubscribe_from_all_notifications_and_keep_making_this_bloody_thing_longer_and_longer_and_longer_and_longer_and_longer 的方法,它在 Ruby 1.8.7 和 1.9.2 中运行良好...
  • 大声笑...谢谢!该程序正在运行,但我不禁想知道我是否搞砸了。
  • 这是一个警告,而不是一个错误 ;) 如果这对您来说很烦人,请关注 oldfartdevelopers 的回答

标签: ruby ruby-on-rails-3 methods


【解决方案1】:

RubyMine 中,您可以通过以下方式更改所有类别的 Ruby 名称的最大长度:

  1. 选择菜单项RubyMine --> 首选项
  2. 在左侧窗格中编辑器下生成的模式对话框中,选择Inspections
  3. 在右侧出现的窗格中,选择 Ruby --> 命名约定
  4. 您有九个类别的 Ruby 名称需要更改。每个都显示最大长度的编辑字段。因此,例如,我将它们从 20 或 30 全部更改为 60。为您更改的九个类别中的每一个点击应用

【讨论】:

    【解决方案2】:

    RubyMine 的谎言 :-) 或者至少不意味着它是 Ruby 解释器的限制。

    looong_name = "a" * 10000; # => "aaaaaaaaa.....
    a_class = Class.new
    a_class.__send__(:define_method, looong_name) { :hello }
    a_class.new.__send__(looong_name) # => :hello
    
    puts a_class.instance_methods.inspect # you better not run this :-)
    

    【讨论】:

      【解决方案3】:

      AFAIK Ruby 中方法名称的大小没有限制。

      您很可能在 RubyMine 中设置了一些编码样式,这会向您显示此警告。如果在 Mac 上检查 RubyMine > Preferences > Code Style > Ruby,看看那里是否有某种偏好。

      只是为了让您知道我也使用 RubyMine,但我没有看到为我设置的这种类型的编码样式。

      【讨论】:

        猜你喜欢
        • 2015-11-24
        • 2011-10-09
        • 1970-01-01
        • 2013-05-31
        • 2010-09-30
        • 1970-01-01
        • 2019-04-26
        • 1970-01-01
        • 2010-10-19
        相关资源
        最近更新 更多