[英]Ruby Class vs Struct
我见过代码库使用结构来包装 class 内的属性和行为。Ruby Class 和结构有什么区别? 什么时候应该用一个代替另一个。?
从结构文档:
Struct 是一种使用访问器方法将多个属性捆绑在一起的便捷方法,而无需编写显式类。
Struct 类生成包含一组成员及其值的新子类。 为每个成员创建类似于 Module#attr_accessor 的读取器和写入器方法。
因此,如果我想要一个可以访问名称属性(读取和写入)的Person
类,我要么通过声明一个类来实现:
class Person
attr_accessor :name
def initalize(name)
@name = name
end
end
或使用结构:
Person = Struct.new(:name)
在这两种情况下,我都可以运行以下代码:
person = Person.new
person.name = "Name"
#or Person.new("Name")
puts person.name
什么时候用?
正如描述所述,当我们需要一组可访问的属性而不必编写显式类时,我们会使用 Structs。
例如,我想要一个点变量来保存 X 和 Y 值:
point = Struct.new(:x, :y).new(20,30)
point.x #=> 20
还有一些例子:
要添加到其他答案中,有些事情你不能用 Struct 做,有些事情你可以。
例如,您不能创建没有参数的 Struct :
Bar = Struct.new
=> ArgumentError: wrong number of arguments (given 0, expected 1+)
Bar = Struct.new(:bar)
bar = Bar.new(nil)
bar.class
=> Bar
但是,一个类可以让你这样做:
class Foo; end
foo = Foo.new
foo.class
=> Foo
您不能为 Struct 参数设置默认值:
Bar = Struct.new(bar: 'default')
=> ArgumentError: unknown keyword: bar
Bar = Struct.new(bar = 'default')
=> NameError: identifier default needs to be constant
但是你可以用一个类来做,要么传递一个散列,要么参数可以按任何顺序排列,甚至丢失:
class Bar
attr_reader :bar, :rab
def initialize(bar: 'default', rab:)
@bar = bar
@rab = rab
end
end
bar = Bar.new(rab: 'mandatory')
bar.rab
=> 'mandatory'
bar.bar
=> 'default'
bar = Bar.new(rab: 'mandatory', bar: 'custom_value')
bar.rab
=> 'mandatory'
bar.bar
=> 'custom_value'
或者直接传递值,如果参数应该以相同的顺序给出,默认的总是在最后:
class Bar
attr_reader :rab, :bar
def initialize(rab, bar = 'default')
@rab = rab
@bar = bar
end
end
bar = Bar.new('mandatory')
bar.rab
=> 'mandatory'
bar.bar
=> 'default'
bar = Bar.new('mandatory', 'custom_value')
bar.rab
=> 'mandatory'
bar.bar
=> 'custom_value'
你不能用 Structs 做任何事情,除非你以这种超级冗长的方式为你的参数设置默认值:
A = Struct.new(:a, :b, :c) do
def initialize(a:, b: 2, c: 3)
super(a, b, c)
end
end
(示例取自这个答案)
您可以在 Struct 中定义方法:
Foo = Struct.new(:foo) do
def method(argument)
# do something with argument
end
end
end
结构对于创建数据对象很有用,就像其中一个答案中提到的点示例一样。
我有时会使用它们以简单的方式在测试中创建假货和模拟。 有时 RSpec allow(foo).to receive(:blah)
等会变得有点冗长,使用 Struct 非常简单。
Struct 是用于创建类的 Ruby 简写。 在适用的情况下使用 Struct 可以简化您的代码。 在https://www.rubytapas.com/2012/11/07/episode-020-struct/ 上有一个很好的讨论
我想@sam_forgot 建议的基准。 这种比较不是很公平。 如今,类和结构都支持关键字参数。 在每个上使用关键字参数会产生相反的效果,正如您从我的示例结构中看到的,带有关键字参数的性能与类没有太大的不同。
require 'benchmark'
REP=1000000
SUser = Struct.new(:name, :age)
SUserK = Struct.new(:name, :age, keyword_init: true)
DATA = { name: "Harry", age: 75 }
DATA2 = DATA.values
class CUser
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
class CUserK
attr_accessor :name, :age
def initialize(name:, age:)
@name = name
@age = age
end
end
Benchmark.bmbm do |x|
x.report 'Struct create and access, without keyword arguments' do
REP.times do
user = SUser.new(DATA)
"#{user.name} - #{user.age}"
end
end
x.report 'Struct create and access, with keyword arguments' do
REP.times do
user = SUserK.new(**DATA)
"#{user.name} - #{user.age}"
end
end
x.report 'Class create and access, without keyword arguments' do
REP.times do
user = CUser.new(*DATA2)
"#{user.name} - #{user.age}"
end
end
x.report 'Class create and access, with keyword arguments' do
REP.times do
user = CUserK.new(DATA)
"#{user.name} - #{user.age}"
end
end
end
Rehearsal ---------------------------------------------------------------------------------------
Struct create and access, without keyword arguments 3.484609 0.011974 3.496583 ( 3.564523)
Struct create and access, with keyword arguments 0.965959 0.005543 0.971502 ( 1.007738)
Class create and access, without keyword arguments 0.624603 0.003999 0.628602 ( 0.660931)
Class create and access, with keyword arguments 0.901494 0.004926 0.906420 ( 0.952149)
------------------------------------------------------------------------------ total: 6.003107sec
user system total real
Struct create and access, without keyword arguments 3.300488 0.010372 3.310860 ( 3.339511)
Struct create and access, with keyword arguments 0.876742 0.004354 0.881096 ( 0.903551)
Class create and access, without keyword arguments 0.553393 0.003962 0.557355 ( 0.568985)
Class create and access, with keyword arguments 0.831672 0.004811 0.836483 ( 0.850224)
有相当大的实际性能差异,ruby 2.6.3p62 中的示例行为:
user system total real
Struct create and access 3.052825 0.005204 3.058029 (3.066316)
Class create and access 0.738605 0.001467 0.740072 (0.743738)
示例代码:
require 'benchmark'
REP=1000000
SUser = Struct.new(:name, :age)
DATA = { name: "Harry", age: 75 }
class User
attr_accessor :name, :age
def initialize(name:, age:)
@name = name
@age = age
end
end
Benchmark.bm 20 do |x|
x.report 'Struct create and access' do
REP.times do
user = SUser.new(DATA)
"#{user.name} - #{user.age}"
end
end
x.report 'Class create and access' do
REP.times do
user = User.new(DATA)
"#{user.name} - #{user.age}"
end
end
end
使用 Struct 工具而不是创建 ruby class 的一些特性如下:
一个 ruby class 需要 8 行来定义所有必要的属性访问器和构造方法,而 Struct 只需要一行代码
将 Struct 分配给变量后,我可以使用类似 Hash 的下标语法获取和设置值,在其中,我可以互换使用符号或字符串作为键
结构有一个相等运算符。 Struct 定义它使得具有相等属性的实例被认为是相等的
与使用 attr_accessor 定义的属性不同,Structs 可以使用 members(将返回实例属性)、each 或 each_pair(您可以在其中迭代属性的名称及其值)等方法自省和迭代它们的属性
结构还包括可枚举,因此我们也有完整的可枚举方法
资料来源: 优美的开发网站
在系统性能方面, 本文肯定类比结构更快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.