Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Ruby 1.9 handle character cases in source code?

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
like image 969
James A. Rosen Avatar asked Aug 22 '08 16:08

James A. Rosen


2 Answers

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

like image 165
Orion Edwards Avatar answered Sep 29 '22 16:09

Orion Edwards


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

like image 22
Julio César Avatar answered Sep 29 '22 15:09

Julio César