Jam*_*sen 5 ruby encoding utf-8
在Ruby 1.8及更早版本中,
Foo
Run Code Online (Sandbox Code Playgroud)
是常量(类,模块或其他常量).而
foo
Run Code Online (Sandbox Code Playgroud)
是一个变量.关键区别如下:
module Foo
bar = 7
BAZ = 8
end
Foo::BAZ
# => 8
Foo::bar
# NoMethodError: undefined method 'bar' for Foo:Module
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但Ruby 1.9 允许使用UTF-8源代码.那么就?这个而言,"大写"还是"低级"呢?那么?(严格的子集)还是?foo?
有一般规则吗?
后来:
Ruby-core已经在考虑一些数学运算符.例如
module Kernel
def ?(num)
...
end
def ?(*args)
...
end
end
Run Code Online (Sandbox Code Playgroud)
会允许的
x = ?2
y = ?(1, 45, ...)
Run Code Online (Sandbox Code Playgroud)
我愿意去看
my_proc = ? { |...| ... }
x ? my_enumerable # same as my_enumerable.include?(x)
my_infinite_range = (1..?)
return 'foo' if x ? y
2.21 ? 2.2
Run Code Online (Sandbox Code Playgroud)
我无法让 IRB 接受 UTF-8 字符,因此我使用了测试脚本 ( /tmp/utf_test.rb)。
“\xce\xbb”作为变量名可以很好地工作:
\n\n# encoding: UTF-8\n\xce\xbb = \'foo\'\nputs \xce\xbb\n\n# from the command line:\n> ruby -KU /tmp/utf_test.rb\nfoo\nRun Code Online (Sandbox Code Playgroud)\n\n“\xce\xbb”也可以用作方法名称:
\n\n# encoding: UTF-8\nKernel.class_eval do\n alias_method :\xce\xbb, :lambda\nend\n\n(\xce\xbb { puts \'hi\' }).call\n\n# from the command line:\n> ruby -KU /tmp/utf_test.rb:\nhi\nRun Code Online (Sandbox Code Playgroud)\n\n但它并不是一个常量:
\n\n# encoding: UTF-8\nObject.const_set :\xce\xbb, \'bar\'\n\n# from the command line:\n> ruby -KU /tmp/utf_test.rb:\nutf_test.rb:2:in `const_set\': wrong constant name \xce\xbb (NameError)\nRun Code Online (Sandbox Code Playgroud)\n\n大写版本也没有:
\n\n# encoding: UTF-8\nObject.const_set :\xce\x9b, \'bar\'\n\n# from the command line:\n> ruby -KU /tmp/utf_test.rb:\nutf_test.rb:2:in `const_set\': wrong constant name \xce\x9b (NameError)\nRun Code Online (Sandbox Code Playgroud)\n\n我怀疑常量名称必须以大写 ASCII 字母开头(必须匹配/^[A-Z]/)。
| 归档时间: |
|
| 查看次数: |
1738 次 |
| 最近记录: |