【问题标题】:How does Ruby 1.9 handle character cases in source code?Ruby 1.9 如何处理源代码中的字符大小写?
【发布时间】:2010-09-06 13:41:44
【问题描述】:

在 Ruby 1.8 及更早版本中,

Foo

是一个常量(类、模块或其他常量)。而

foo

是一个变量。主要区别如下:

module Foo
  bar = 7
  BAZ = 8
end

Foo::BAZ
# => 8

Foo::bar
# NoMethodError: undefined method 'bar' for Foo:Module

这一切都很好,但是 Ruby 1.9 allows UTF-8 source code。就这一点而言, 是“大写”还是“小写”? (严格子集)或Ɖfoo呢?

有一般规则吗?

稍后:

Ruby-core 已经在考虑一些数学运算符。例如

module Kernel
  def √(num)
    ...
  end
  def ∑(*args)
    ...
  end
end

允许

x = √2
y = ∑(1, 45, ...)

我很想看看

my_proc = λ { |...| ... }

x ∈ my_enumerable  # same as my_enumerable.include?(x)

my_infinite_range = (1..∞)

return 'foo' if x ≠ y

2.21 ≈ 2.2

【问题讨论】:

    标签: ruby encoding utf-8


    【解决方案1】:

    好吧,我开玩笑的回答不太顺利。

    This mailing list question, with answer from Matz 表示 Ruby 1.9 内置的 String#upcaseString#downcase 方法将只处理 ASCII 字符。

    如果不自己进行测试,我会认为这是强有力的证据,表明源代码中的所有非 ascii 字符都可能被视为小写。

    有人可以下载和编译最新的 1.9 看看吗?

    【讨论】:

      【解决方案2】:

      如果你在源代码中使用扩展的 UTF8 字符作为标识符,我不知道 ruby​​ 会做什么,但我知道我会做什么,那就是拍你的后脑勺并告诉你不要'这样做

      【讨论】:

        【解决方案3】:

        我很想看看

        my_proc = λ { |...| ... }
        
        x ∈ my_enumerable  # same as my_enumerable.include?(x)
        
        my_infinite_range = (1..∞)
        
        return 'foo' if x ≠ y
        
        2.21 ≈ 2.2
        

        我希望看到有人尝试在英文键盘上键入该程序:P

        【讨论】:

        • 您的担心是绝对有道理的,但我很乐意构建 Textmate 宏来将 memberof + tab 转换为 ∈。打字不比 include? 短,但我认为代码更清楚地说明了我的意思。我确定这是口味问题。我真的很喜欢我在大学里的离散数学课。
        【解决方案4】:

        Ruby 1.9.2-p0 (YARV) 中,结果与原始帖子中的结果相同(即 Foo::bar #=> # NoMethodError: undefined method 'bar' for Foo :模块)。此外,遗憾的是,带有重音的字母不被视为大写字母或小写字母,相关方法不会产生任何结果。

        例子:

        "á".upcase
        => "á"
        "á" == "Á".downcase
        => false
        

        【讨论】:

          【解决方案5】:

          我无法让 IRB 接受 UTF-8 字符,因此我使用了测试脚本 (/tmp/utf_test.rb)。

          “λ”作为变量名可以正常工作:

          # encoding: UTF-8
          λ = 'foo'
          puts λ
          
          # from the command line:
          > ruby -KU /tmp/utf_test.rb
          foo
          

          “λ”也可以作为方法名使用:

          # encoding: UTF-8
          Kernel.class_eval do
            alias_method :λ, :lambda
          end
          
          (λ { puts 'hi' }).call
          
          # from the command line:
          > ruby -KU /tmp/utf_test.rb:
          hi
          

          不过,它不能作为常数工作:

          # encoding: UTF-8
          Object.const_set :λ, 'bar'
          
          # from the command line:
          > ruby -KU /tmp/utf_test.rb:
          utf_test.rb:2:in `const_set': wrong constant name λ (NameError)
          

          大写版本也没有:

          # encoding: UTF-8
          Object.const_set :Λ, 'bar'
          
          # from the command line:
          > ruby -KU /tmp/utf_test.rb:
          utf_test.rb:2:in `const_set': wrong constant name Λ (NameError)
          

          我怀疑常量名称必须以大写 ASCII 字母开头(必须匹配 /^[A-Z]/)。

          【讨论】:

            猜你喜欢
            • 2010-12-27
            • 1970-01-01
            • 2011-07-20
            • 1970-01-01
            • 1970-01-01
            • 2014-05-04
            • 2011-03-10
            • 2021-08-14
            • 1970-01-01
            相关资源
            最近更新 更多