如何在 Ruby 中定义 class 级常量正则表达式以供外部 classes 使用
How to define class level constant regular expressions in Ruby to be used by external classes
我正在编写一些测试来验证要在 Ruby 控制台应用程序中使用的某些正则表达式的行为。我正在尝试在不打算实例化的 class 上定义常量 class 级别字段(只是应该在其上定义常量 RE 值。我无法使用 [=37 正确定义它=] 习语(我有 C++/C# 背景)。
首先我尝试定义一个class常量
class Expressions
# error is on following line (undefined method DATE)
Expressions.DATE = /(?<Year>\d{4})-(?<Month>\d{2})-(?<Day>\d{2})/
end
class MyTest < Test::Unit::TestCase
def setup
@expression = Expressions::DATE
end
def test
assert "1970-01-01" =~ @expression
end
end
这只会产生错误:Expressions:Class (NoMethodError)
的未定义方法“DATE=”
接下来我尝试了class属性:
class Expressions
@@Expressions.DATE = /(?<Year>\d{4})-(?<Month>\d{2})-(?<Day>\d{2})/
end
class MyTest < Test::Unit::TestCase
def setup
# NameError: uninitialized constant Expressions::DATE here:
@expression = Expressions::DATE
end
def test
assert "1970-01-01" =~ @expression
end
end
这会产生一个 NameError: uninitialized constant Expressions::DATE 错误。
我知道我可以在 class 上定义属性以用作实例,但这是低效的并且不是解决问题的正确方法(只是一个技巧)。 (在 C++ 中我会使用静态常量,完成)
所以我真的卡住了。我需要知道在 Ruby 中定义需要在其他 class 中使用的常量正则表达式的正确方法是什么。我对定义、初始化及其使用有疑问,
谢谢。
A constant in Ruby is like a variable, except that its value is supposed to
remain constant for the duration of a program. The Ruby interpreter does not
actually enforce the constancy of constants, but it does issue a warning if a
program changes the value of a constant. Lexically, the names of constants look
like the names of local variables, except that they begin with a capital letter.
By convention, most constants are written in all uppercase with underscores to
separate words, LIKE_THIS. Ruby class and module names are also constants, but
they are conventionally written using initial capital letters and camel case,
LikeThis.
The Ruby Programming Language: David Flanagan; Yukihiro Matsumoto.
这应该有效:
class Expressions
DATE = /.../
end
class MyTest < Test::Unit::TestCase
def setup
@expression = Expressions::DATE
end
# ...
end
我正在编写一些测试来验证要在 Ruby 控制台应用程序中使用的某些正则表达式的行为。我正在尝试在不打算实例化的 class 上定义常量 class 级别字段(只是应该在其上定义常量 RE 值。我无法使用 [=37 正确定义它=] 习语(我有 C++/C# 背景)。
首先我尝试定义一个class常量
class Expressions
# error is on following line (undefined method DATE)
Expressions.DATE = /(?<Year>\d{4})-(?<Month>\d{2})-(?<Day>\d{2})/
end
class MyTest < Test::Unit::TestCase
def setup
@expression = Expressions::DATE
end
def test
assert "1970-01-01" =~ @expression
end
end
这只会产生错误:Expressions:Class (NoMethodError)
的未定义方法“DATE=”接下来我尝试了class属性:
class Expressions
@@Expressions.DATE = /(?<Year>\d{4})-(?<Month>\d{2})-(?<Day>\d{2})/
end
class MyTest < Test::Unit::TestCase
def setup
# NameError: uninitialized constant Expressions::DATE here:
@expression = Expressions::DATE
end
def test
assert "1970-01-01" =~ @expression
end
end
这会产生一个 NameError: uninitialized constant Expressions::DATE 错误。
我知道我可以在 class 上定义属性以用作实例,但这是低效的并且不是解决问题的正确方法(只是一个技巧)。 (在 C++ 中我会使用静态常量,完成)
所以我真的卡住了。我需要知道在 Ruby 中定义需要在其他 class 中使用的常量正则表达式的正确方法是什么。我对定义、初始化及其使用有疑问,
谢谢。
A constant in Ruby is like a variable, except that its value is supposed to remain constant for the duration of a program. The Ruby interpreter does not actually enforce the constancy of constants, but it does issue a warning if a program changes the value of a constant. Lexically, the names of constants look like the names of local variables, except that they begin with a capital letter. By convention, most constants are written in all uppercase with underscores to separate words, LIKE_THIS. Ruby class and module names are also constants, but they are conventionally written using initial capital letters and camel case, LikeThis.
The Ruby Programming Language: David Flanagan; Yukihiro Matsumoto.
这应该有效:
class Expressions
DATE = /.../
end
class MyTest < Test::Unit::TestCase
def setup
@expression = Expressions::DATE
end
# ...
end