Ruby 1.9如何处理源代码中的字符大小写? [英] How does Ruby 1.9 handle character cases in source code?

查看:144
本文介绍了Ruby 1.9如何处理源代码中的字符大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby 1.8及更早版本中,

  Foo 

是一个常数(一个类,一个模块或另一个常量)。而$ / pre
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

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

 模块Foo 
bar = 7
BAZ = 8
end

Foo :: BAZ
#=> 8

Foo :: bar
#NoMethodError:Foo的未定义方法bar:

这很好,但Ruby 1.9 允许UTF-8源代码。就这个而言,大写还是低分? (严格子集)或Ɖfoo



是否有一般规则?



稍后:



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

 模块内核
def√(num)
...
end
defΣ(* args)
...
end
end

将允许

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

我很想看到

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

x∈my_enumerable#与my_enumerable.include相同(x)

my_infinite_range =(1..∞)

return' foo'如果x≠y

2.21≈2.2


解决方案

我不能让IRB接受UTF-8字符,所以我用了一个测试脚本( /tmp/utf_test.rb )。 >

λ作为变量名称正常工作:

 #encoding:UTF -8 
λ='foo'
放置λ

#从命令行:
> ruby -KU /tmp/utf_test.rb
foo

λ也可以正常工作一个方法名称:

 #encoding:UTF-8 
Kernel.class_eval do
alias_method: :lambda
end

(λ{puts'hi'})。从命令行调用


> ruby -KU /tmp/utf_test.rb:
hi

它不起作用一个常数,但是:

 #encoding:UTF-8 
Object.const_set:λ,'bar'

#从命令行:
> ruby -KU /tmp/utf_test.rb:
utf_test.rb:2:在`const_set'中:错误的常量名称λ(NameError)

也没有大写的版本:

 #encoding:UTF-8 
Object.const_set:Λ,'bar'

#从命令行:
> ruby -KU /tmp/utf_test.rb:
utf_test.rb:2:在`const_set'中:错误的常量名称Λ(NameError)

我的怀疑是,常数名称必须以大写ASCII字母开头(必须匹配 / ^ [AZ] / )。 p>

In Ruby 1.8 and earlier,

Foo

is a constant (a Class, a Module, or another constant). Whereas

foo

is a variable. The key difference is as follows:

module Foo
  bar = 7
  BAZ = 8
end

Foo::BAZ
# => 8

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

That's all well and good, but Ruby 1.9 allows UTF-8 source code. So is "uppercase" or "lowecase" as far as this is concerned? What about (strict subset) or Ɖfoo?

Is there a general rule?

Later:

Ruby-core is already considering some of the mathematical operators. For example

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

would allow

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

I would love to see

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

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

my_infinite_range = (1..∞)

return 'foo' if x ≠ y

2.21 ≈ 2.2

解决方案

I can't get IRB to accept UTF-8 characters, so I used a test script (/tmp/utf_test.rb).

"λ" works fine as a variable name:

# encoding: UTF-8
λ = 'foo'
puts λ

# from the command line:
> ruby -KU /tmp/utf_test.rb
foo

"λ" also works fine as a method name:

# 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

It doesn't work as a constant, though:

# 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)

Nor does the capitalized version:

# 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)

My suspicion is that constant names must start with a capital ASCII letter (must match /^[A-Z]/).

这篇关于Ruby 1.9如何处理源代码中的字符大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆