How does Ruby 1.9 handle character cases in source

2019-06-21 20:21发布

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

5条回答
\"骚年 ilove
2楼-- · 2019-06-21 20:52

I don't know what ruby would do if you used extended UTF8 characters as identifiers in your source code, but I know what I would do, which would be to slap you upside the back of the head and tell you DON'T DO THAT

查看更多
迷人小祖宗
3楼-- · 2019-06-21 20:58

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]/).

查看更多
可以哭但决不认输i
4楼-- · 2019-06-21 21:08

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 would love to see someone trying to type that program on an English keyboard :P

查看更多
We Are One
5楼-- · 2019-06-21 21:17

In Ruby 1.9.2-p0 (YARV) the result is the same as in the original post (i.e., Foo::bar #=> # NoMethodError: undefined method 'bar' for Foo:Module). Also, letters with accent are unfortunately not considered as being upper nor lower and related methods produce no result.

Examples:

"á".upcase
=> "á"
"á" == "Á".downcase
=> false
查看更多
兄弟一词,经得起流年.
6楼-- · 2019-06-21 21:18

OK, my joking answer didn't go down so well.

This mailing list question, with answer from Matz indicates that Ruby 1.9's built in String#upcase and String#downcase methods will only handle ASCII characters.

Without testing it myself, I would see this as strong evidence that all non-ascii characters in source code will likely be considered lowercase.

Can someone download and compile the latest 1.9 and see?

查看更多
登录 后发表回答