TL;DR:不,这是不可能的......而且很长的回答,是的,有可能,阅读元编程部分:)
Ruby 是一种动态语言,这就是为什么您不会像使用 C# 之类的语言那样收到编译时类型的警告/错误。
与不能为变量指定类型一样,也不能为attr_accessor 指定类型。
对于来自 .NET 的您来说,这可能听起来很愚蠢,但是在 Ruby 社区中,人们有点期望您编写测试。如果你这样做,这些类型的问题将基本消失。在 Ruby on Rails 中,您应该测试您的模型。如果你这样做了,你真的不会有任何不小心分配错误的麻烦。
如果您专门讨论 Ruby on Rails 中的 ActiveRecord,将 String 分配给在数据库中定义为 Integer 的属性将导致抛出异常。
顺便说一下,按照惯例,属性不应该使用CamelCase,所以正确的类定义应该是
class Person
attr_accessor :first_name
attr_accessor :last_name
attr_accessor :home_address
end
class Address
attr_accessor :address_line1
attr_accessor :city
attr_accessor :country
end
这样做的一个原因是,如果您将第一个字母大写,Ruby 将定义一个常量而不是变量。
number = 1 # regular variable
Pi = 3.14159 # constant ... changing will result in a warning, not an error
元编程技巧
顺便说一句,Ruby 还具有极其强大的元编程功能。您可以编写自己的 attr_accessor 并进行类型检查,可以使用类似
typesafe_accessor :price, Integer
定义 something like
class Foo
# 'static', or better said 'class' method ...
def self.typesafe_accessor(name, type)
# here we dynamically define accessor methods
define_method(name) do
# unfortunately you have to add the @ here, so string interpolation comes to help
instance_variable_get("@#{name}")
end
define_method("#{name}=") do |value|
# simply check a type and raise an exception if it's not what we want
# since this type of Ruby block is a closure, we don't have to store the
# 'type' variable, it will 'remember' it's value
if value.is_a? type
instance_variable_set("@#{name}", value)
else
raise ArgumentError.new("Invalid Type")
end
end
end
# Yes we're actually calling a method here, because class definitions
# aren't different from a 'running' code. The only difference is that
# the code inside a class definition is executed in the context of the class object,
# which means if we were to call 'self' here, it would return Foo
typesafe_accessor :foo, Integer
end
f = Foo.new
f.foo = 1
f.foo = "bar" # KaboOoOoOoM an exception thrown here!
或者至少是这样的:) 这段代码可以工作! Ruby 允许您动态定义方法,这就是attr_accessor 的工作方式。
块也是几乎总是闭包,这意味着我可以在不将其作为参数传递的情况下执行if value.is_a? type。
这里解释的太复杂了,什么时候是真的,什么时候不是。总之,有不同类型的块
-
Proc,由Proc.new创建
-
lambda,由关键字lambda创建
其中一个区别是在 lambda 中调用 return 只会从 lambda 本身返回,但是当您从 Proc 执行相同操作时,块周围的整个方法将返回,即迭代时使用,例如
def find(array, something)
array.each do |item|
# return will return from the whole 'find()' function
# we're also comparing 'item' to 'something', because the block passed
# to the each method is also a closure
return item if item == something
end
return nil # not necessary, but makes it more readable for explanation purposes
end
如果您喜欢这类东西,我建议您查看PragProg Ruby Metaprogramming screencast。